如何使用StringEntity将JSONarray发送到Android服务器? [英] How to send JSONarray to server in Android using StringEntity?
本文介绍了如何使用StringEntity将JSONarray发送到Android服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从Android向服务器发送数据。有一些String值和一个名为multiples的JSONarray
I'm trying to send data to server from Android. There are some String values and a JSONarray called "multiples"
我如何调用方法
String id = new AddContactRequest().execute(token, etNameS, etLastNameS, etTitleS, etCompanyS, etUrlS, etDescriptionS, txtStatusS, "1", etAddressS, multiplesUpdatedArray.toString()).get();
这是我发送数据的方法
public class AddContact extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
String line;
line = postData(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9], params[10]);
return line;
}
protected void onPostExecute(String result){
super.onPostExecute(result);
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);
}
public String postData(String token, String name, String lastname, String title, String company, String url, String description, String status, String campaign, String address, String multiples) {
//public String postData(String token, String name, String lastname, String title, String company, String phone, String email, String url, String description, String status, String campaign, String address) {
String line2 = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://fake.com/api/insert");
try {
JSONObject params = new JSONObject();
params.put("token", token);
params.put("name", name);
params.put("lastname", lastname);
params.put("title", title);
params.put("company", company);
params.put("url", url);
params.put("description", description);
params.put("address", address);
params.put("status", status);
params.put("campaign", campaign);
params.put("multiples", multiples);
httppost.setEntity(new StringEntity(params.toString(), HTTP.UTF_8));
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity httpEntity = response.getEntity();
line2 = EntityUtils.toString(httpEntity);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return line2;
}
}
回复是错误代码500,内部服务器错误。我不知道什么是幸福或我做错了什么。
The response is an error code 500, internal server error. I don't know what is happining or what I'm doing wrong.
当我使用 BasicNameValuePair
时,所有数据都被发送,除了我的倍数JSONArray
When I use BasicNameValuePair
all data is sent except my "multiples" JSONArray
请帮忙。提前致谢!
推荐答案
将JSONArray转换为String&发送到服务器
Convert JSONArray to String & Send it to Server
StringEntity entity = new StringEntity(jsonArray.toString());
这篇关于如何使用StringEntity将JSONarray发送到Android服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文