AndroidでのJSON解析で、以下の形式のデータをhttp URLに投稿したいので、
webdata={"email":"test@test.com","password":"123456"}
このJSONのhttppostを使用してこのデータをhttp urlに投稿するにはどうすればよいですか?
まず、json形式のデータを準備し、それをGETまたはPOSTのいずれかの要件に従ってリクエストとして送信します。これは、Asynctaskを使用して行う必要があります。
JSONObject jObject = new JSONObject();
try{
jObject.put("email",urvalue);
jObject.put("password",urvalue);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("webdata", jObject.toString()));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}catch(Exception e){}
StringEntity
クラスを使用します。
StringEntity entity = new StringEntity("Your JSON String",HTTP.UTF_8);
そしてそれをあなたに追加しますHttpPost
httpPost.setEntity(entity);