我想将以下JSON请求发送到Web服务并阅读响应。
{"Email":"aaa@tbbb.com","Password":"123456"}
我知道如何阅读JSON。问题是上面的JSON对象必须以变量名json发送。我希望将Json对象作为带有get请求的参数发送。
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
HttpGet httpget = new HttpGet(FinalURL.toString());
//HttpPost request = new HttpPost(serverUrl);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
我怎么能从android做到这一点?有哪些步骤,例如创建请求对象,设置内容标题等等。
答案 0 :(得分:0)
您可以使用以下方式设置请求:
httpget.setEntity(new JSONObject(requestString));
答案 1 :(得分:0)
我现在使用的其中一个选项是RequestParams:
它可以实现为:
RequestParams rp = new RequestParams();
rp.put("Email", "aaa@tbbb.com");
rp.put("Password", "123456");
Utils.client.get(url, rp, new AsyncHttpResponseHandler() {
//or your http code
http://loopj.com/android-async-http/doc/com/loopj/android/http/RequestParams.html
http://loopj.com/android-async-http/