Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 is soo slow, and gets the error, the only thing the website does it opens the php document and start the bash command. :( so it should redirect only 1 time

Thanks :)

I will switch the lights on with a button in the app:

    public void switchLightOn(View view) {
    Context context = getApplicationContext();
    int duration = Toast.LENGTH_SHORT;
    CharSequence text = "An";
    Toast.makeText(context, text, duration).show();

    URL url11 = null;
    try {
        url11 = new URL("http://192.168.2.104/homepage/steuerung/steckdosen.php?id=1&status=1");
    } catch (Exception e) {
        e.printStackTrace();
    }
    new OpenUrlConnection().execute(url11);

}

public void switchLightOff(View view) {
    // ausschalten
    Context context = getApplicationContext();
    CharSequence text = "Aus";
    int duration = Toast.LENGTH_SHORT;
    Toast.makeText(context, text, duration).show();

    URL url10 = null;
    try {
        url10 = new URL("http://192.168.2.104/homepage/steuerung/steckdosen.php?id=1&status=0");
    } catch (Exception e) {
        e.printStackTrace();
    }

    new OpenUrlConnection().execute(url10);

}

private class OpenUrlConnection extends AsyncTask<URL, Void, Integer> {

    public Integer doInBackground(URL... urls) {
            try {
                HttpURLConnection urlConnection = (HttpURLConnection) urls[0].openConnection();
                InputStream in = urlConnection.getInputStream();
                InputStreamReader isw = new InputStreamReader(in);
            } catch (Exception e) {
                e.printStackTrace();
            }
        return 1;
    }
}

EDIT:

now i am using:

                    HttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(String.valueOf(urls[0]));
                HttpResponse response = httpclient.execute(httpget);
                httpclient.getConnectionManager().shutdown();

but get these errors:

05-06 18:10:36.803  30939-31047/de.carsten.raspicontrol D/dalvikvm﹕ GC_FOR_ALLOC freed 239K, 2% free 17052K/17324K, paused 10ms, total 10ms
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ org.apache.http.client.ClientProtocolException
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at de.carsten.raspicontrol.RasPiControl$OpenUrlConnection.doInBackground(RasPiControl.java:103)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at de.carsten.raspicontrol.RasPiControl$OpenUrlConnection.doInBackground(RasPiControl.java:97)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'http://192.168.2.104/homepage/steuerung/steckdosen.php?id=1&status=0'
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at org.apache.http.impl.client.DefaultRedirectHandler.getLocationURI(DefaultRedirectHandler.java:173)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:923)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:475)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-06 18:10:38.163  30939-31047/de.carsten.raspicontrol W/System.err﹕ ... 10 more
like image 281
Steakschen Avatar asked Mar 19 '23 16:03

Steakschen


1 Answers

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.

like image 68
Aleks G Avatar answered Apr 26 '23 12:04

Aleks G