错误500 HttpUrlConnection java

时间:2017-10-25 17:22:52

标签: java android httpurlconnection

我正在使用AsyncHttpClient向我的数据库发出一些请求,而且工作正常。但是,当我尝试在手机上使用3g时,它根本无效。这就是我决定将HttpClient更改为HttpUrlConnection的原因(人们说3g + wifi效果更好)。但是,当我编写代码时,我只是陷入了无法解决的缓冲问题。代码是:

public class SendPostRequest extends AsyncTask<String, Void, String>{
    String base64CredenciaisCodificadas;
    HashMap postDataParam;
    @Override
    protected String doInBackground(String... url) {
        String response = "";
        try{
            URL link = new URL(url[0]);
            HttpURLConnection e = (HttpURLConnection)link.openConnection();
            e.setReadTimeout(15000);
            e.setConnectTimeout(15000);
            e.setRequestMethod("POST");
            e.setRequestProperty("Authorization", "Basic " + base64CredenciaisCodificadas);
            e.setDoInput(true);
            e.setDoOutput(true);
            e.setFixedLengthStreamingMode(getPostDataString(postDataParam).length());
            OutputStream os = e.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParam));
            writer.flush();
            writer.close();
            os.close();
            int responseCode = e.getResponseCode();
            if (responseCode == 200){
                //Code to get data from web service.
            }
        } catch(Exception e){

        }
        return response;
    }
    private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        Iterator var4 = params.entrySet().iterator();

        while(var4.hasNext()) {
            Map.Entry entry = (Map.Entry)var4.next();
            if(first) {
                first = false;
                result.append("?");
            } else {
                result.append("&");
            }

            result.append(URLEncoder.encode((String)entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode((String)entry.getValue(), "UTF-8"));
        }

        return result.toString();
    }
}

错误日志是:

10-25 15:16:11.128 30807-30898/petma.testesappcarona I/System.out: Stat: 500; Msg: buffer(com.android.okhttp.internal.http.HttpConnection$UnknownLengthSource@834db8).inputStream()

如果有人有任何疑问,请询问,我会发送更多数据。

编辑:这是android。它有权在互联网上显示。

2 个答案:

答案 0 :(得分:1)

常见原因:Steam因错误的参数或其他未处理的异常而关闭。

可能的原因:HashMap postDataParam永远不会在此代码中分配,因为您在下面的代码中传递null。

      e.setFixedLengthStreamingMode(
       getPostDataString(postDataParam).length());

null参数导致 com.android.okhttp.internal.http.HttpConnection$UnknownLengthSource@834db8).inputStream()

免费提供建议

  • 尝试在可能的地方重用代码

    you could have stored getPostDataString(postDataParam) in some variable since its getting used twice in your code.
    
  • 如果可能,请使用改装进行网络通话。

答案 1 :(得分:0)

我解决了这个问题。这在代码中没有问题。我尝试连接的Web服务只能通过GET请求访问。我将POST切换到GET,现在可以了!

但是,现在有一个新问题:当切换到3g时,我得到403:禁止访问。有什么想法吗?