您好我的问题是我的应用程序速度很慢这个错误:
java.net.ProtocolException: Too many redirects: 21
现在我不知道为什么它太慢了,并且得到错误,网站唯一做的就是打开php文档并启动bash命令。 :(所以它应该只重定向一次
谢谢:)
我将使用应用程序中的按钮打开灯:
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;
}
}
编辑:
现在我正在使用:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(String.valueOf(urls[0]));
HttpResponse response = httpclient.execute(httpget);
httpclient.getConnectionManager().shutdown();
但是得到这些错误:
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
答案 0 :(得分:4)
你的Android代码非常好 - 它是不能打球的服务器。首先,检查您是否正在调用正确的URL并传递正确的参数。如果所有这些都很好,那么问题肯定在服务器端。
如果您自己开发了服务器代码,请在此处发布,我们会尝试提供帮助。如果它是其他人的代码,那么您必须让他们修复它,提供URL和错误的详细信息。
答案 1 :(得分:2)
使用HttpClient
连接类
org.apache.http.client.HttpClient
而不是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;
答案 2 :(得分:1)
我遇到了同样的问题。即使我花了很多时间来解决这个问题。我发现问题正在发生之后:当您调用某些JSON服务时,有时服务可能会以原始格式或格式返回数据,这可能不是典型的application/json
。您的.openConnection()
或InputStreamReader
可能无法读取响应标头和JSON数据。为了解决这个问题,我尝试了以下,它对我有用。
使用HttpClient httpClient = new DefaultHttpClient();
代替
(HttpURLConnection) obj.openConnection();
设置允许循环重定向:
httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS,
true);
设置以下帖子标题很重要。
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");
使用StringEntity
使用UTF-8读取输入流:
httpresponse.getEntity().getContent(),HTTP.UTF_8), 8);
以下是适用于我的示例代码:
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);