Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误500 HttpUrlConnection java_Java_Android_Httpurlconnection - Fatal编程技术网

错误500 HttpUrlConnection java

错误500 HttpUrlConnection java,java,android,httpurlconnection,Java,Android,Httpurlconnection,我使用AsyncHttpClient向我的数据库发出一些请求,它工作正常。但是,当我尝试在手机上使用3g时,它根本不起作用。这就是为什么我决定将HttpClient改为HttpUrlConnection(人们说它与3g+wifi配合使用效果更好)。但是,当我编写它的时候,我遇到了一个无法解决的缓冲区问题。代码是: public class SendPostRequest extends AsyncTask<String, Void, String>{ String base6

我使用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();
    }
}
如果有任何人有任何疑问,请询问,我将发送更多数据


编辑:它是android。它有权在清单上上网。

常见原因:蒸汽被错误的参数或其他未处理的异常关闭

可能原因:此代码中从未分配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.
    
  • 如果可能,对网络呼叫使用改装

    • 我解决了这个问题。代码上没有问题。我试图连接的web服务只能通过GET请求访问。我换了一个帖子,现在可以了


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

      这是安卓上的?如果是,您是否在Android清单上授予了访问internet的权限?是的,它在Android中,并且有“
      500
      code-这是一个服务器problem@DimaKozhevin否,他使用AsyncHttpClient
      5xx服务器错误测试正常-服务器未能满足请求。
      并查看状态代码定义
      you could have stored getPostDataString(postDataParam) in some variable since its getting used twice in your code.