java - 如何使用restclient将字典传递到android中的Web服务?

标签 java android wcf hashmap

我已经通过将字典集合传​​递给方法来调用 Web 应用程序中的 wcf 服务。我想使用 ksoap2 在 android 中实现同样的事情。

这是我通过它的方式,

 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 错误请求

最佳答案

使用

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

关于java - 如何使用restclient将字典传递到android中的Web服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19313870/

相关文章:

java - 使用 Edittext 更改 Textview 的文本?

Java 日志文件包含重复的记录

java - Android:上下文菜单不显示 ListView 的成员由 LinearLayout 定义?

java - 在另一个元素中使用显式等待查找元素

wcf - 如何让 Entity Framework 和 WCF 与事务一起工作?好吧……有什么 secret ?

c# - Java解密返回的某些 block 有错误

java - 更有效地读取按字母顺序排序的文本文件

android - 应用内购买 Titanium Android

c# - QueryStringConverter 因未知原因不工作

wcf - 有没有办法减少 WCF 占用的带宽?