2017-03-01 161 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()); 
相關問題