Skip to content

Commit

Permalink
can get http response
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichi10 committed Aug 25, 2015
1 parent 6da677b commit 6568b3e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -29,20 +30,34 @@ public class HttpCommunication {
//Post parameters
List<NameValuePair> postParams = new ArrayList<NameValuePair>();

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

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

private void setServerName(String str){
//Uri for get
mBuilder.scheme("http");
mBuilder.encodedAuthority(serverName);
mBuilder.path("/"+path+"/");
mBuilder.encodedAuthority(str);
}

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

//set parameter for get
Expand All @@ -57,30 +72,35 @@ public void setPostParameter(String key1, String 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");
}
new Thread(new Runnable() {
@Override
public void run() {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(mBuilder.build().toString());
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();
}
});
Log.d("test", result);
}catch (ClientProtocolException e){
throw new RuntimeException(e);
}catch (IOException e){
throw new RuntimeException(e);
}finally {
httpClient.getConnectionManager().shutdown();
}
}
}).start();
}

public void Post() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public void onClick(View v) {
startActivityForResult(intent, this.mRequestCode);
break;
case R.id.doSignUp:
String server = "www.drk7.jp";
String path = "weather/xml/27.xml";
HttpCommunication hc = new HttpCommunication(server, path);
hc.Get();
break;
}
}
Expand Down

0 comments on commit 6568b3e

Please sign in to comment.