我想通過我的Android應用發送這樣的信息:Android:如何使用http post發送用戶輸入值?
https://myserver/index.php?x0=param1&y0=param2&z0=param3
其中參數1,參數2和參數3的值的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
在服務器端,你沒有得到改變的值或什麼? – Vamshi
是的,沒有錯誤味精,但在服務器上的變化我沒有我期望的值... – user2748484
在doinbackground中打印x00,y00,z00的值,並看到這些值是否更新 – Vamshi