android HttpPost中的org.apache.http.client.ClientProtocolException

时间:2015-03-19 13:57:39

标签: android http-post

您好我正在尝试通过在我的应用程序中调用HttpPost从服务器获取数据,它之前工作正常,但现在它无法正常工作(我没有对代码进行任何更改)并且给予&# 34; org.apache.http.client.ClientProtocolException"错误。

我不知道会出现什么问题,但在收到错误后我在google上搜索并执行了一些额外的行,如下所示,但仍未得到响应同样的错误。

public static String postRequest(String url, List<NameValuePair> data) {

    String result = "";
    try {

        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(data));

        StringEntity se = new StringEntity(HTTP.UTF_8);
        se.setContentType("text/xml");
        httpPost.setEntity(se);

        HttpParams httpParameters = new BasicHttpParams();

        int timeoutConnection = 10000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);

        int timeoutSocket = 10000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);           
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);


        BasicHttpResponse httpResponse = (BasicHttpResponse) httpClient
                .execute(httpPost);


        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity);
            result = result.trim();
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();

    } catch (ParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {

        return "-333" + e.toString();

    }
    return result;
}

它给了我以下错误:

03-19 19:10:17.852: W/System.err(21707): org.apache.http.client.ClientProtocolException
03-19 19:10:17.877: W/System.err(21707):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:750)
03-19 19:10:17.877: W/System.err(21707):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519)
03-19 19:10:17.882: W/System.err(21707):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497)
03-19 19:10:17.882: W/System.err(21707):    at com.example.asyntasks.Utility.postRequest(Utility.java:284)
03-19 19:10:17.882: W/System.err(21707):    at com.example.asyntasks.GetHomeItemsTask.getHomeRecipe(GetHomeItemsTask.java:88)
03-19 19:10:17.882: W/System.err(21707):    at com.example.asyntasks.GetHomeItemsTask.doInBackground(GetHomeItemsTask.java:54)
03-19 19:10:17.882: W/System.err(21707):    at com.example.asyntasks.GetHomeItemsTask.doInBackground(GetHomeItemsTask.java:1)
03-19 19:10:17.882: W/System.err(21707):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-19 19:10:17.882: W/System.err(21707):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-19 19:10:17.882: W/System.err(21707):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-19 19:10:17.887: W/System.err(21707):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-19 19:10:17.887: W/System.err(21707):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-19 19:10:17.887: W/System.err(21707):    at java.lang.Thread.run(Thread.java:864)
03-19 19:10:17.887: W/System.err(21707): Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'http://www.example.com/'
03-19 19:10:17.887: W/System.err(21707):    at org.apache.http.impl.client.DefaultRedirectHandler.getLocationURI(DefaultRedirectHandler.java:173)
03-19 19:10:17.887: W/System.err(21707):    at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:932)
03-19 19:10:17.892: W/System.err(21707):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:483)
03-19 19:10:17.892: W/System.err(21707):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:748)
03-19 19:10:17.892: W/System.err(21707):    ... 12 more
03-19 19:10:17.902: W/System.err(21707): org.json.JSONException: End of input at character 0 of 
03-19 19:10:17.902: W/System.err(21707):    at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
03-19 19:10:17.902: W/System.err(21707):    at org.json.JSONTokener.nextValue(JSONTokener.java:97)
03-19 19:10:17.907: W/System.err(21707):    at org.json.JSONObject.<init>(JSONObject.java:154)
03-19 19:10:17.907: W/System.err(21707):    at org.json.JSONObject.<init>(JSONObject.java:171)
03-19 19:10:17.907: W/System.err(21707):    at com.example.asyntasks.GetHomeItemsTask.getHomeRecipe(GetHomeItemsTask.java:90)
03-19 19:10:17.907: W/System.err(21707):    at com.example.asyntasks.GetHomeItemsTask.doInBackground(GetHomeItemsTask.java:54)
03-19 19:10:17.907: W/System.err(21707):    at com.example.asyntasks.GetHomeItemsTask.doInBackground(GetHomeItemsTask.java:1)
03-19 19:10:17.907: W/System.err(21707):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-19 19:10:17.907: W/System.err(21707):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-19 19:10:17.907: W/System.err(21707):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-19 19:10:17.907: W/System.err(21707):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-19 19:10:17.927: W/System.err(21707):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-19 19:10:17.927: W/System.err(21707):    at java.lang.Thread.run(Thread.java:864)

请帮帮我。

0 个答案:

没有答案