代码之家  ›  专栏  ›  技术社区  ›  Sajeetharan

如何使用restclient将Dictionary传递到android中的web服务?

  •  1
  • Sajeetharan  · 技术社区  · 11 年前

    我已经通过将字典集合传递给一个方法,完成了在web应用程序中调用wcf服务。我想在android中使用ksoap2实现同样的功能。

    这是我传球的方式,

     HttpClient httpclient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 30000);
        HttpConnectionParams.setSoTimeout(httpclient.getParams(), 50000);
        JSONObject InputDic = new JSONObject();
        JSONObject ProfileDictionary = new JSONObject();
        JSONArray InputDicArray = new JSONArray(); 
        JSONArray ProfileDicArray = new JSONArray(); 
        //Input Dictionary to the Common Service
        try{
        InputDic.put("GURefID", 0);
        InputDic.put("ProfileType","Customer");
        InputDic.put("SecurityToken","");
        InputDic.put("SkillID", 1);
        InputDic.put("WorkflowServerID", 1);
        InputDic.put("XMPPUserName", "testuser");    
        InputDic.put("XMPPPassword", "testuser");
        InputDic.put("XMPPServerName", "test");      
        InputDic.put("GUTranID", 12130);
        //Input Profile Dictionary
        EditText txtProfileCode = (EditText)findViewById(R.id.edittextprofilecode);
        ProfileDictionary.put("ProfileCode", txtProfileCode.getText().toString().trim());
        EditText guAddressID = (EditText)findViewById(R.id.edittextcustcode);            
        ProfileDictionary.put("guAddressID", txtProfileCode.getText().toString().trim());
        String ProfileTitle =   title.getItemAtPosition(title.getSelectedItemPosition()).toString();
        ProfileDictionary.put("Title", ProfileTitle.toString().trim());
        ProfileDictionary.put("Gender", "Male");
        EditText txtCustName = (EditText)findViewById(R.id.edittextCustname);           
        ProfileDictionary.put("FirstName",txtCustName.getText().toString().trim()  );
        EditText txtPhone = (EditText)findViewById(R.id.edittextPhoneNo);       
        ProfileDictionary.put("Phonenumber", txtPhone.getText().toString().trim());
        EditText txtMobile = (EditText)findViewById(R.id.edittextMobileno);    
         ProfileDictionary.put("Mobilenumber",  txtMobile.getText().toString().trim());
         EditText txtfax = (EditText)findViewById(R.id.edittextfax);    
         ProfileDictionary.put("Faxnumber",  txtfax.getText().toString().trim());
         EditText txtemail = (EditText)findViewById(R.id.edittextemail);    
         ProfileDictionary.put("E-mail",  txtemail.getText().toString().trim());
         EditText txtskype = (EditText)findViewById(R.id.edittextskype);    
         ProfileDictionary.put("Skypeid",  txtskype.getText().toString().trim());
         EditText txtgtalk = (EditText)findViewById(R.id.edittextgtalk);    
         ProfileDictionary.put("Googletalkid",  txtgtalk.getText().toString().trim());
         EditText txtTwitterid = (EditText)findViewById(R.id.edittexttwitter);    
         ProfileDictionary.put("Twitterid",  txtTwitterid.getText().toString().trim());
         EditText txtfacebook = (EditText)findViewById(R.id.edittextfacebook);    
         ProfileDictionary.put("Facebookid",  txtfacebook.getText().toString().trim());
         InputDicArray.put(InputDic);
         ProfileDicArray.put(ProfileDictionary);
        // Create a new HttpClient and Post Header
        }
        catch(Exception e){
        }
    
    
    
        try {
           JSONObject InputMessage = new JSONObject();
    
            JSONObject mParams = new JSONObject();
            mParams.put("InputDictionary", InputDicArray);
            mParams.put("AdditionalDictionary1", ProfileDicArray);
            mParams.put("UseAdditionalDictionary", true);
            mParams.put("AdditionalDictionaryName1","ProfileDictionary");
            mParams.put("SecurityToken", 323);
            mParams.put("SessionID", 2423);
            mParams.put("UseWorkflowID",true); 
            mParams.put("UseGUSessionID",true);
            mParams.put("WorkflowID",0);
            mParams.put("WorkflowType", "");
            mParams.put("WorkflowName", "Profile");
            InputMessage.put("DuoWorkflowInvokeProxy",mParams);
            HttpPost httppost = new HttpPost(URL);
            StringEntity se = new StringEntity(InputMessage.toString(), "UTF-8");
    
            se.setContentType("application/json;charset=UTF-8");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_LEN, String.valueOf(InputMessage.toString().length())));
            httppost.setHeader("Content-type", "text/xml"); 
            httppost.setHeader("Accept", "text/xml"); 
            HttpResponse response = httpclient.execute(httppost);
             BufferedReader reader = new BufferedReader(new InputStreamReader(
                 response.getEntity().getContent(), "UTF-8"));
                            String sResponse;
                            StringBuilder s = new StringBuilder();
    
                            while ((sResponse = reader.readLine()) != null) {
                                s = s.append(sResponse);
                            }
                        System.out.println("Response: ");
                        Log.v("hari", "Response : ");
            }
        catch(Exception e)
        {
    
                e.printStackTrace(); 
    
        }
    

    我收到一个错误,如下所示

    HTTP/1.1 400错误请求

    1 回复  |  直到 11 年前
        1
  •  0
  •   Himanshu Joshi    11 年前

    使用

    httpost.setHeader("Accept", "application/json;odata=verbose"); 
    
    httpost.setHeader("Content-type", "application/json;odata=verbose"); 
    

    相反

    httpost.setHeader("Accept", "application/json"); 
    
    httpost.setHeader("Content-type", "application/json"); 
    

    因为V3有效负载目前不支持application/json