我正在尝试通过我的 Android 应用发送此消息:
https://myserver/index.php?x0=param1&y0=param2&z0=param3
其中 param1、param2 和 param3 是用户在 EditText 字段中输入的值...
我的应用程序目前读取这些值,但是当我尝试使用 httppost 发送它们时,它不起作用(我没有错误消息,但值并没有像他们应该做的那样改变......)
有人可以解释我做错了什么吗?
这是我的 MainActivity 的一部分,我在其中读取参数的值并调用 sendPostRequest 方法:
private OnClickListener InitialPosListener = new OnClickListener() {
@Override
public void onClick(View v) {
String x00 = InitialPosX.getText().toString(); String y00 = InitialPosY.getText().toString(); String z00 = InitialPosZ.getText().toString();
float x0 = Float.valueOf(x00); float y0 = Float.valueOf(y00); float z0 = Float.valueOf(z00);
sendPostRequest(x00, y00, z00);
};
这是 http post 方法,稍后在我的 MainActivity 中定义:
private void sendPostRequest(String x00, String y00, String z00) {
class SendPostReqAsyncTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
String x00 = params[0];
String y00 = params[1];
String z00 = params[2];
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://myserver/index.php");
BasicNameValuePair XBasicNameValuePair = new BasicNameValuePair("x0", x00);
BasicNameValuePair YBasicNameValuePAir = new BasicNameValuePair("y0", y00);
BasicNameValuePair ZBasicNameValuePAir = new BasicNameValuePair("z0", z00);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(XBasicNameValuePair);
nameValuePairList.add(YBasicNameValuePAir);
nameValuePairList.add(ZBasicNameValuePAir);
try {
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
httpPost.setEntity(urlEncodedFormEntity);
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while((bufferedStrChunk = bufferedReader.readLine()) != null){
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
System.out.println("First Exception caz of HttpResponese :" + cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Second Exception caz of HttpResponse :" + ioe);
ioe.printStackTrace();
}
} catch (UnsupportedEncodingException uee) {
System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
uee.printStackTrace();
}
return null;
}
} //END CLASS SendPostReqAsyncTask
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(x00, y00, z00);
} //END VOID sendPostRequest