我正在尝试通过在 Http post 方法中设置参数来从服务器获取数据(如此处所述)
当参数直接在url中设置时效果很好,但是当我尝试使用setEntity(new UrlEncodedFormEntity(nameValuePairs))
服务器设置参数时返回空白答案(就像我没有定义任何变量一样)。这是我的doInBackground()
代码:
String inMsg = "";
String url = "xxx";
HttpClient client = new DefaultHttpClient();
DataOutputStream dataOutputStream = null;
BufferedReader input = null;
HttpPost post = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("retrieve", "test"));
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
if (isCancelled())
{
publishProgress(CANCELLED);
return (null);
}
input = new BufferedReader( new InputStreamReader (entity.getContent(), "iso-8859-1"), 8);
StringBuilder inptemp = new StringBuilder();
while ((inMsg = input.readLine()) != null)
{
inptemp.append(inMsg + "\n");
}
publishProgress(SUCCESS);
if (isCancelled())
{
publishProgress(CANCELLED);
return (null);
}
inMsg = "";
inMsg = inptemp.toString();
Log.w("params", "msg: "+inMsg);
Log.w("params", url);
JSONArray jArray = new JSONArray(inMsg);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.w("log_tag","id: "+json_data.getInt("Id")
);
//Get an output to the screen
//return String += "\n\t" + jArray.getJSONObject(i);
}
有什么建议么 ?