2016-03-01 79 views
0

Android新手,webservice调用返回状态码400.我怀疑这是由于传递参数的方式不正确。我需要将它们作为JSON对象传递,但不知道该怎么做?如何将webservice参数作为JSON对象传递?

下面应该是参数并正常工作。

enter image description here

在我的Android代码,其表示状态代码400

(更新为下面的代码)

RequestParams params = new RequestParams(); 


    Map<String, String> map = new HashMap<String, String>(); 
map.put("login", "WT"); map.put("password", "03"); 
params.put("query", map); params.put("includeUserMiscInfo", "true"); 

client.post("http://XXXX/SDService_SAFTI/ServiceSD.svc/LoginUser",params,new AsyncHttpResponseHandler() { 

我也使用如下JSON对象尝试。

protected void sendJson(final String email, final String pwd) { 
     Thread t = new Thread() { 

      public void run() { 
       Looper.prepare(); //For Preparing Message Pool for the child Thread 
       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
       HttpResponse response; 
       JSONObject json = new JSONObject(); 

       try { 
        HttpPost post = new HttpPost("http://192.168.0.102/SDService_SAFTI/ServiceSD.svc/LoginUser"); 
        Query queryObj = new Query(); 
        queryObj.setLogin("WT"); 
        queryObj.setPassword("3"); 


        json.put("Query", queryObj); 
//     json.put("email", email); 
//     json.put("password", pwd); 
        json.put("includeUserMiscInfo", true); 


        StringEntity se = new StringEntity(json.toString()); 
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
        post.setEntity(se); 
        response = client.execute(post); 

        /*Checking response */ 
        if(response!=null){ 


         InputStream in = response.getEntity().getContent(); //Get the data in the entity 
         Toast.makeText(getActivity().getApplicationContext(), "Response:" + convertStreamToString(in),Toast.LENGTH_LONG).show(); 

        } 

       } catch(Exception e) { 
        e.printStackTrace(); 
//     getActivity().createDialog("Error", "Cannot Estabilish Connection"); 
       } 

       Looper.loop(); //Loop in the message queue 
      } 
     }; 

     t.start(); 
    } 

    private static String convertStreamToString(InputStream is) { 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append((line + "\n")); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 

但它返回 - 请求错误XML

任何建议都欢迎。提前谢谢了。

+0

我的问题在这里得到解决 - > http://stackoverflow.com/questions/35738165/passing-json-obj-as-parameter-in-webservice-returns-bad-request-response/35738591#35738591 – Dep

回答

0

您确定自己的网址http://192.168.0.102/DService/ServiceSD.svc/LoginUser?Query=login=WT&password=03&includeUserMiscInfo=true是正确的吗?

+0

http ://192.168.0.102/XXXXXXX/XXXXSD.svc/LoginUser是正确的。我不知道如何传递参数?我也尝试使用Requestparams。请参阅高级客户端网址(正确)。 – Dep

+0

您的更新代码看起来正确。你仍然得到400错误或其他东西(logcat中的任何错误)? –

+0

仍然相同的状态码= 400。错误的请求。 Android Monitor没有错误。 – Dep

0

您可以使用以下方法来调用Web服务:

public void makeHTTPCall() { 

     RequestParams params = new RequestParams(); 
     String query = "[login=WT&password=03]" 
     params.put("query", query); 
     params.put("includeUserMiscInfo", "true"); 

     prgDialog.setMessage(getResources().getString(R.string.please)); 
     AsyncHttpClient client = new AsyncHttpClient(); 

     client.post("http://192.168.0.102/SDService_SAFTI/ServiceSD.svc/LoginUser", 
       params, new AsyncHttpResponseHandler() { 

      @Override 
      public void onSuccess(String response) { 
       // Hide Progress Dialog 
       prgDialog.hide();  

// do your code if result is success 

      } 


      @Override 
      public void onFailure(int statusCode, Throwable error, 
        String content) { 

       prgDialog.hide(); 

       if (statusCode == 404) { 
        Toast.makeText(getApplicationContext(), 
          "Requested resource not found", 
          Toast.LENGTH_LONG).show(); 
       } 

       else if (statusCode == 500) { 
        Toast.makeText(getApplicationContext(), 
          "Something went wrong at server end", 
          Toast.LENGTH_LONG).show(); 
       } 

       else { 
        Toast.makeText(
          getApplicationContext(), 
          getResources().getString(R.string.some_error_occured) 
          , Toast.LENGTH_LONG) 
          .show(); 
       } 
      } 
     }); 
    } 

希望这会帮助你。

+0

,看截图。我的webservices参数并不那么简单。查询里面有2个项目 - 登录名和密码。然后includeUserMiscInfo是另一个参数。 – Dep

+0

其实你传递的格式不正确。所以试着像这样传递它。看到我的编辑 – KishuDroid

+0

试过这个,但仍然是相同的状态代码/错误。 – Dep

相关问题