【问题标题】:Post webservice is not working in Android发布网络服务在 Android 中不起作用
【发布时间】:2017-07-12 19:40:31
【问题描述】:

我是 Android 和网络服务的新手。我从未使用过邮政网络服务。尝试如下,但它既没有显示成功也没有失败。

如果我使用高级的 rest 客户端 chrome 扩展,我可以测试并且 web 服务运行良好,只是需要很长时间。

当尝试从代码运行时,它立即显示 - 在调用 webservices 之后 - toast msg --> 表示它没有调用 webservice。

花了 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();
    }

【问题讨论】:

    标签: android web-services


    【解决方案1】:

    您没有将参数设置为任何内容,也没有发送它们。 另外,您正在尝试进行 post call,但 url 带有 get 参数:“http://43.555.6.111:8081/SIPServices/SIc.svc/Checkout?barcode=B12&patron=thi

    还可以考虑实现 onStart 方法并记录它,看看它是否启动了。

    【讨论】:

    【解决方案2】:

    这是我处理请求的自定义类。我在这里使用 json,你可以返回并输入你想要的。你只需要

    • 网址:http://43.555.6.111:8081/SIPServices/SIc.svc/Checkout
    • 方法:“POST”
    • params: 键值数组列表:barcode=B12&patron=thi

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

      public JSONObject makeHttpRequest(String url, String 方法, HashMap 参数){

          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;
      }
      

    【讨论】:

      猜你喜欢
      • 2016-08-24
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      相关资源
      最近更新 更多