2017-03-01 163 views
0

我想从Android发送数据到服务器。有一些字符串值和JSONarray称为“倍数”如何使用StringEntity将JSONarray发送到Android中的服务器?

我如何调用该方法

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,内部服务器错误。我不知道什么是开心或者我做错了什么。

当我使用BasicNameValuePair的所有数据,除了我的“倍数” JSONArray

请帮忙发送。提前致谢!

+0

1.数据也被发送,但它是一个字符串不是一个数组。 2.你得到了服务器的响应,但服务器无法处理它,所以看看服务器端(如果可能的话),错误500可以是任何事情。 – Fusselchen

回答

2

转换JSONArray到字符串&它发送给服务器

StringEntity entity = new StringEntity(jsonArray.toString()); 
相关问题