【发布时间】:2014-01-31 14:26:08
【问题描述】:
我想用 Android 向一个 url 发送一个 http 请求。
我是一名 iOS 开发人员,现在正在尝试学习 Android。
我曾经在 iOS 中发送JSON 字符串,如下所示
{"function":"login", "parameters": {"username": "nithin""password": "123456"}}
如何在 Android 中发送此内容?
我试过List<NameValuePair>,但找不到合适的解决方案。
我尝试过的完整代码 -
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("function", "login"));
nameValuePairs.add(new BasicNameValuePair("username", "nithin"));
nameValuePairs.add(new BasicNameValuePair("password", "123456"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
【问题讨论】: