java.net.ProtocolException: Too many redirects: 21 Android App

后端 未结 3 1691
梦毁少年i
梦毁少年i 2020-12-18 09:08

Hello i got the Problem that my app is so slow with this error:

java.net.ProtocolException: Too many redirects: 21

Now i dont know why it i

相关标签:
3条回答
  • 2020-12-18 09:14

    Your Android code is perfectly fine - it's the server that's not playing ball. First of all, check that you are calling the correct URL and passing the correct parameters. If all of these are fine, then the problem is definitely on the server side.

    If you developed the server code yourself, then post it here and we'll try to help. If it's somebody else's code, then you have to get them to fix it, providing the details of the URL and the error.

    0 讨论(0)
  • 2020-12-18 09:24

    Use HttpClient connection class

    org.apache.http.client.HttpClient
    

    instead of HttpURLConnection

    HttpClient httpClient = new DefaultHttpClient();
    httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); 
    HttpPost httpPost = new HttpPost("http:\\www.myweb.com"); 
    HttpResponse response = httpclient.execute(httppost); 
    responseBody = "";
    BufferedReader buffer = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String s = "";
    while ((s = buffer.readLine()) != null) 
      responseBody += s;
    
    0 讨论(0)
  • 2020-12-18 09:25

    I was facing same issue. Even I spent quite a lot amount of time to fix this issue. I found out that issue was coming to due following: When you make a call to some JSON services, sometime services might return you data in raw formats or format which may not be typical application/json. Your .openConnection() or InputStreamReader may not be able to read reponse headers and JSON Data. To fix this issue I tried following and it worked for me.

    • Used HttpClient httpClient = new DefaultHttpClient(); instead of (HttpURLConnection) obj.openConnection();

    • Set allow circular redirect: httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

    • Set following post headers which is important.

      httpPost.setHeader("charset","utf-8"); 
      httpPost.setHeader("Accept", "application/json"); 
      httpPost.setHeader("Accept-Language","en-US,en;q=0.8");
      httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); 
      
    • Use StringEntity

    • Read input stream with UTF-8:

      httpresponse.getEntity().getContent(),HTTP.UTF_8), 8); 
      

    Here is the sample code which worked for me:

    HttpClient httpClient = new DefaultHttpClient(); 
    String url =http://....;
    httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); 
    HttpPost httpPost = new HttpPost(url); 
    httpPost.setHeader("Content-Type", "application/json"); 
    httpPost.setHeader("charset","utf-8"); 
    httpPost.setHeader("Accept", "application/json"); 
    httpPost.setHeader("Accept-Language","en-US,en;q=0.8");
    httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); 
    //or you can try httpPost.setContentType("application/x-www-form-urlencoded"); 
    StringEntity requestBody = new StringEntity(jsonBody);
    requestBody.setContentType("application/json");
    httpPost.setEntity(requestBody); HttpResponse httpresponse = httpClient.execute(httpPost); 
    org.apache.http.StatusLine statusRespons = httpresponse.getStatusLine(); 
    if ( statusRespons.getStatusCode() > 201 ) { 
        errorText = statusRespons.getStatusCode() + " : " + statusRespons.toString() + " : " +statusRespons.getReasonPhrase() ; 
    } 
    BufferedReader buffer = new BufferedReader(new InputStreamReader(httpresponse.getEntity().getContent(),HTTP.UTF_8), 8); 
    String s = ""; while ((s = buffer.readLine()) != null) 
    jsonString.append(s);
    
    0 讨论(0)
提交回复
热议问题