-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
app/src/main/java/pmv02/ppr/yuichi10/github/com/joinevents/DataManage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(){}; | ||
} |
97 changes: 97 additions & 0 deletions
97
app/src/main/java/pmv02/ppr/yuichi10/github/com/joinevents/HttpCommunication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |