API的post方法无效。 NewTask.execute()给出错误“无法解析符号”。如果我删除此NewTask.execute(),API不返回任何内容。
makePostRequestAsyncTask NewTask = new makePostRequestAsyncTask(); NewTask.execute();
我有两个问题:
为什么给出无法解决符号错误?
为什么在删除NewTask.execute()时没有返回任何内容?
p.s:我正确使用异步任务吗?由于我是初学者,所以无法确定。感谢。
public void makePostRequest()
{
class makePostRequestAsyncTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost =
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// log exception
e.printStackTrace();
}
//making POST request.
try {
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String response_body = reader.readLine();
Log.d("Http Post Response:", String.valueOf(response));
// HttpResponse response = httpClient.execute(httpPost);
// write response to log
// Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// Log exception
e.printStackTrace();
} catch (IOException e) {
// Log exception
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equals("working")){
Toast.makeText(getApplicationContext(), "HTTP POST is working...", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Invalid POST req...", Toast.LENGTH_LONG).show();
}
}
makePostRequestAsyncTask NewTask= new makePostRequestAsyncTask();
NewTask.execute();
}
}
答案 0 :(得分:0)
您正在AsyncTask
内初始化并执行。
}
makePostRequestAsyncTask NewTask= new makePostRequestAsyncTask();
NewTask.execute();
}// AsyncTask your class close here
代替意味着更接近AsyncTask
}
}// AsyncTask your class close here
makePostRequestAsyncTask NewTask= new makePostRequestAsyncTask();
NewTask.execute();