ClientProtocolException - Android - php

时间:2012-05-14 14:25:59

标签: java php client

我习惯自己解决问题,但这次我真的很难过。如果有人能给我一个线索,那就太好了。

问题: 我的android应用程序调用一个php脚本(比如script1.php),它使用“header”php函数重定向另一个脚本(比如script2.php)。

如果我在浏览器中运行http://xxxxxx.com/script1.php?param1=y&param2=y2&param3=y3它可以正常运行,但如果应用程序运行以下代码则会崩溃。

    nameValuePairs.add(new BasicNameValuePair("param1",param1));
    nameValuePairs.add(new BasicNameValuePair("param2",param2));
    nameValuePairs.add(new BasicNameValuePair("param3",param3));;


    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://xxxxxx.com/script1.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        Log.v("println", "response: "+response);
    }catch(Exception e1){
        Log.e("log_tag","Error in http connection"+e1.toString());
    }

我检查了一切。 Script1.php从Android应用程序中获取正确的参数但是那个抛出一个org.apache.http.client.ClientProtocolException

2 个答案:

答案 0 :(得分:0)

问题可能出在Php脚本上。请确保您的脚本以邮寄或请求方式接受您的请求。

答案 1 :(得分:0)

这应该有效

nameValuePairs.add(new BasicNameValuePair("param1",param1));
nameValuePairs.add(new BasicNameValuePair("param2",param2));
nameValuePairs.add(new BasicNameValuePair("param3",param3));;


try{
    HttpParams httpParams=new BasicHttpParams();
    HttpClient httpclient = new DefaultHttpClient(httpParams);
    HttpPost httppost = new HttpPost("http://xxxxxx.com/script1.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    Log.v("println", "response: "+response);
}catch(Exception e1){
    Log.e("log_tag","Error in http connection"+e1.toString());
}