我习惯于自己解决问题,但这次我真的被困住了。如果有人能给我一个线索,那就太好了。
问题:我的安卓应用程序调用一个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
问题可能出在Php Script上。请确保您的脚本接受您的请求作为发布或请求方法。
这应该有效
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());
}