Skip to content

Commit

Permalink
connection pert
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichi10 committed Aug 25, 2015
1 parent 80908a7 commit 6da677b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@
<!-- 位置情報 ネットワークから -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<!-- to connect internet -->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pmv02.ppr.yuichi10.github.com.joinevents;

/**
* Created by yuichi on 8/25/15.
*/
public class DataManage {
public void DataManege(){};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package pmv02.ppr.yuichi10.github.com.joinevents;

import android.content.Entity;
import android.net.Uri;
import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Created by yuichi on 8/25/15.
*/
public class HttpCommunication {
Uri.Builder mBuilder;
String mUri;
//Post parameters
List<NameValuePair> postParams = new ArrayList<NameValuePair>();

public void HttpCommunication(String serverName, String path){
//Uri for get
mBuilder = new Uri.Builder();
mBuilder.scheme("http");
mBuilder.encodedAuthority(serverName);
mBuilder.path("/" + path + "/");
//Uri for post
mUri = "http:" + serverName + "/" + path + "/";
}

public void setURL(String serverName, String path){
mBuilder.scheme("http");
mBuilder.encodedAuthority(serverName);
mBuilder.path("/"+path+"/");
}

//set parameter for get
public void setGetParameter(String key1, String key2) {
mBuilder.appendQueryParameter(key1, key2);
}

//set parameter for post
public void setPostParameter(String key1, String key2){
postParams.add(new BasicNameValuePair(key1, key2));
}

//try to do get
public void Get(){
HttpGet request = new HttpGet(mBuilder.build().toString());
DefaultHttpClient httpClient = new DefaultHttpClient();
try{
String result = httpClient.execute(request, new ResponseHandler<String>() {
@Override
public String handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException {
switch (httpResponse.getStatusLine().getStatusCode()){
case HttpStatus.SC_OK:
return EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
case HttpStatus.SC_NOT_FOUND:
throw new RuntimeException("There are no data");
default:
throw new RuntimeException("something wrong");
}
}
});
Log.d("test", result);
}catch (ClientProtocolException e){
throw new RuntimeException(e);
}catch (IOException e){
throw new RuntimeException(e);
}finally {
httpClient.getConnectionManager().shutdown();
}
}

public void Post() {
HttpPost request = new HttpPost(mUri);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse res = null;
try {
request.setEntity(new UrlEncodedFormEntity(postParams, "utf-8"));
res = httpClient.execute(request);
}catch (IOException e){
e.printStackTrace();
}
}
}

0 comments on commit 6da677b

Please sign in to comment.