我试图通过在Http post方法中设置参数来获取服务器中的数据(如提到的here)
当params直接在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);
}
有什么建议吗?