2016-03-01 218 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

相關問題