2016-02-06 49 views
-1

我是Android新手,還有webservices。我從來沒有使用過發佈web服務。嘗試如下,但它既不顯示成功也不失敗。Post webservice在Android中不工作

如果我使用高級rest客戶端chrome擴展,我能夠測試和webservice完美地工作,只是它需要很多時間。

當試圖從代碼運行時,它立即顯示 - 在調用webservices之後 - toast msg - >表示它沒有調用web服務。

花了2天,但沒有運氣。有什麼建議麼?

public void borrowBook(String barCode, String patronId) 
    { 
     final int DEFAULT_TIMEOUT = 200000 * 1000000000; 

     // Make RESTful webservice call using AsyncHttpClient object 
     AsyncHttpClient client = new AsyncHttpClient(); 
     client.setTimeout(DEFAULT_TIMEOUT); 

     progress.setMessage("Please Wait..."); 
     progress.setIndeterminate(false); 
     progress.setCancelable(false); 
     progress.show(); 

     RequestParams params = new RequestParams(); 

     Toast.makeText(getActivity().getApplicationContext(), "B4 calling webservice", Toast.LENGTH_LONG).show(); 

     client.post("http://43.555.6.111:8081/SIPServices/SIc.svc/Checkout?barcode=B12&patron=thi", new TextHttpResponseHandler() { 
        @Override 
        public void onSuccess(int i, Header[] headers, String response) { 
         Toast.makeText(getActivity().getApplicationContext(), "Response: " + response, Toast.LENGTH_LONG).show(); 
         Log.d("TAG", "Success"); 
        } 

        @Override 
        public void onFailure(int statusCode, Header[] headers, String response, Throwable error) { 
         Toast.makeText(getActivity().getApplicationContext(), "Status code :" + statusCode + "errmsg : " + error.getMessage(), Toast.LENGTH_LONG).show(); 
         Toast.makeText(getActivity().getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show(); 
         Log.d("TAG", "Failure"); 
        } 

       } 
     ); 
     progress.dismiss(); 
     Toast.makeText(getActivity().getApplicationContext(), "After calling webservice", Toast.LENGTH_LONG).show(); 
    } 

回答

1

您不會將參數設置爲任何值,也不會發送它們。 另外,你正在嘗試做一個post調用,但URL是與獲取參數:「http://43.555.6.111:8081/SIPServices/SIc.svc/Checkout?barcode=B12&patron=thi

也考慮實施onStart方法並記錄它,看看它是否開始。

+0

我試着分別傳遞參數,但仍然無法正常工作。 client.post(「http://43.555.6.222:8091/Sices/SIc.svc/Checkout」 ,params,new TextHttpResponseHandler(){ – Dep

+0

)也歡迎任何其他建議。 – Dep

0

這是我的自定義類來處理請求。我在這裏使用json,你可以返回並鍵入你想要的。所有你需要的是

  • 網址:http://43.555.6.111:8081/SIPServices/SIc.svc/Checkout
  • 方法: 「POST」
  • PARAMS:鍵和值的ArrayList:條碼= B12 &靠山= THI

    HashMap<String, String> mValueParams = new HashMap<>(); 
    mValueParams.put("barcode", "B12"); 
    mValueParams.put("patron", "thi"); 
    
    JSONObject json = jsonParser.makeHttpRequest(url, "POST",mValueParams); 
    

    公衆的JSONObject makeHttpRequest(String url,String method, HashMap params){

    sbParams = new StringBuilder(); 
        int i = 0; 
        for (String key : params.keySet()) { 
         try { 
          if (i != 0) { 
           sbParams.append("&"); 
          } 
          sbParams.append(key).append("=") 
            .append(URLEncoder.encode(params.get(key), charset)); 
    
         } catch (UnsupportedEncodingException e) { 
          e.printStackTrace(); 
         } 
         i++; 
        } 
    
        if (method.equals("POST")) { 
         // request method is POST 
         try { 
          urlObj = new URL(url); 
    
          conn = (HttpURLConnection) urlObj.openConnection(); 
    
          conn.setDoOutput(true); 
    
          conn.setRequestMethod("POST"); 
    
          conn.setRequestProperty("Accept-Charset", charset); 
    
          conn.setReadTimeout(10000); 
          conn.setConnectTimeout(15000); 
    
          conn.connect(); 
    
          paramsString = sbParams.toString(); 
    
          wr = new DataOutputStream(conn.getOutputStream()); 
          wr.writeBytes(paramsString); 
          wr.flush(); 
          wr.close(); 
    
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } else if (method.equals("GET")) { 
         // request method is GET 
    
         if (sbParams.length() != 0) { 
          url += "?" + sbParams.toString(); 
         } 
    
         try { 
          urlObj = new URL(url); 
    
          conn = (HttpURLConnection) urlObj.openConnection(); 
    
          conn.setDoOutput(false); 
    
          conn.setRequestMethod("GET"); 
    
          conn.setRequestProperty("Accept-Charset", charset); 
    
          conn.setConnectTimeout(15000); 
    
          conn.connect(); 
    
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
    
        } 
    
        try { 
         //Receive the response from the server 
         int status = conn.getResponseCode(); 
         InputStream in = new BufferedInputStream(conn.getInputStream()); 
         BufferedReader reader = new BufferedReader(new InputStreamReader(in, "iso-8859-1"), 8); 
         result = new StringBuilder(); 
         String line; 
         while ((line = reader.readLine()) != null) { 
          result.append(line); 
         } 
    
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
    
        conn.disconnect(); 
    
        // try parse the string to a JSON object 
        try { 
         jObj = new JSONObject(result.toString()); 
        } catch (Exception e) { 
         Log.e("JSON Parser", "Error parsing data " + e.toString()); 
        } 
    
        // return JSON Object 
        return jObj; 
    }