1

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

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

如果我在浏览器中运行http://xxxxxx.com/script1.php?param1=y¶m2=y2¶m3=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

4

2 回答 2

0

问题可能出在 PHP 脚本上。请确保您的脚本以 post 或 request 方法接受您的请求。

于 2012-08-08T06:28:24.440 回答
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());
}
于 2014-01-07T19:49:20.947 回答