「Android」クライアント側にpostリクエストを実装するコード

/**
* サーバーにリクエスト
*
* @param model
* @param paramList
* @return
*/
private static JSONObject doPost(int model, List<NameValuePair> paramList) {

HttpPost httpPost = new HttpPost(INNER_URL);
HttpEntity entity = null;
try {
entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
httpPost.setEntity(entity);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}

HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse != null
&& httpResponse.getStatusLine().getStatusCode() == 200) {
String element = EntityUtils.toString(httpResponse.getEntity(),
HTTP.UTF_8);
if (element.startsWith(“{“)) {
try {
return new JSONObject(element);
} catch (JSONException e) {
e.printStackTrace();
}
}
}

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return null;

}

}

Comments are closed.