2013-07-01 52 views
0

我是一個新手,並不知道Android的很多。 我需要發送一個arrayList的userdefined類到web服務使用http post。我可以使用nameValuePairs發送一個實例。但不知道如何在android中發送2個更多的實例? 這裏是我的代碼如何從WCF的web服務的POST方法發送類的Arraylist從Android

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     String[] array = params[0]; 
     String stringifiedResponse = null; 
     HttpResponse response = null; 
     for (int i = 0; i < array.length; i++) { 

      nameValuePairs.add(new BasicNameValuePair("userid", share 
        .getUserId())); 
      nameValuePairs.add(new BasicNameValuePair("email", array[i])); 
      nameValuePairs 
        .add(new BasicNameValuePair("contact_number", "")); 
      nameValuePairs.add(new BasicNameValuePair("name", "")); 
     } 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost(Constants.URL 
       + "reward.php?status=add"); 
     String result = null; 
     try { 

      post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      response = client.execute(post); 
      stringifiedResponse = EntityUtils 
        .toString(response.getEntity()); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     Log.d("RESPONSE", "" + stringifiedResponse); 

這隻發送最後一個條目我想給所有條目。請幫忙。

回答

0
use jackson library to create json string from array list of class object and after that send it. I have done successfully with this. 


public String sendArrayListOfuserclass() { 

     String request_string = null; 
     HttpClient client = new DefaultHttpClient(); 
     HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout 
     HttpResponse response; 
     try { 
      HttpPost post = new HttpPost(
        ServiceConstant.ALL_PERSON_UPDATE_DETAIL_URL); 
      post.setHeader("Content-type", "application/x-www-form-urlencoded"); 
      DatabaseHelper mDatabaseHelper = DatabaseHelper 
        .getInstance(sContext); 
      List<PersonInformationBean> allPersonDetailList = mDatabaseHelper 
        .getAllPersonDetail(); 
      List<ActivationBean2> PersonRequest = new ArrayList<ActivationBean2>(); 
      int size = allPersonDetailList.size(); 
      if (size > 0) { 
       for (PersonInformationBean personInformationBean : allPersonDetailList) { 
        ActivationBean2 personactivationdata = new ActivationBean2(); 
        try { 
         personactivationdata.setPersonID(personInformationBean 
           .getPersonId()); 
         personactivationdata 
           .setActivationKey(personInformationBean 
             .getActivationKey()); 
        } catch (Exception e) { 
        } 
        PersonRequest.add(personactivationdata); 
       } 
      } 
      RefreshPersonBean personwithdeviceid = new RefreshPersonBean(); 
      personwithdeviceid.setPersonRequest(PersonRequest); 
      personwithdeviceid.setDeviceID(MyPreferences.getInstance(sContext) 
        .getDeviceId()); 
      request_string = convertBeanToJson(personwithdeviceid); 
      request_string = request_string.replace("\\", "") 
        .replace("\"[", "[").replace("]\"", "]"); 
      request_string = request_string.replace("\n", "").replace("\r", ""); 
      Log.e("referesh person request", request_string); 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
      nameValuePairs.add(new BasicNameValuePair(
        ServiceConstant.RequestData, request_string)); 
      post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); 
      // post.setEntity(new ByteArrayEntity(json.toString().getBytes())); 
      response = client.execute(post); 
      /* Checking response */ 
      if (response != null) { 
       InputStream inputStream = response.getEntity().getContent(); // Get 
       request_string = convertStreamToString(inputStream); 
       Log.e("response", 
         "response of post = " + request_string.toString()); 
       return request_string; 
      } 
     } catch (IOException e) { 
      request_string = "timeout"; 
      e.printStackTrace(); 
     } catch (Exception e) { 
      // Toast.makeText(sContext, "Server Error", 
      // Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
     return request_string; 
    } 



this is jackson library function: 

private String convertBeanToJson(RefreshPersonBean bean) { 
     String reqQuery = null; 
     try { 
      reqQuery = mapperObj.writeValueAsString(bean); 
      Log.d("convertBeanToJson(): request query is:", reqQuery); 
     } catch (JsonGenerationException e) { 
      Log.e("convertBeanToJson():", " caught JsonGenerationException"); 
     } catch (JsonMappingException e) { 
      // mELogger.error("convertBeanToJson(): caught JsonMappingException"); 
     } catch (IOException e) { 
      Log.e("convertBeanToJson():", " caught IOException"); 
     } 
     return reqQuery; 
    }// 

library name jackson-all-1.6.2 
相關問題