我在AsyncTask中调用了2个Web服务方法。第一个被成功调用但第二个Web服务方法未被调用。知道如何在第一个方法之后调用第二个方法吗?
AsyncTask代码
public class Transaction extends AsyncTask<String,Void,String>
{
private String finalBalance1 = "", price1 = "", pName = "", pNric = "", pClass = "", sno = "";
public Transaction(String price1, String pName, String pNric, String pClass, String sno, String finalBalance1)
{
super();
this.finalBalance1 = Double.toString(finalBalance);
this.price1 = Double.toString(sPrice);
this.pName = sRA.studentName;
this.pNric = sRA.studentNric;
this.pClass = sRA.studentClass;
this.sno = logger.stallNo;
}
@Override
protected String doInBackground (String...url)
{
String result = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://152.226.152.175/NCO/WebService.asmx/InsertStudentTransaction");
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("NRIC", pNric));
nameValuePairs.add(new BasicNameValuePair("Name", pName));
nameValuePairs.add(new BasicNameValuePair("Class", pClass));
nameValuePairs.add(new BasicNameValuePair("StallNo", sno));
nameValuePairs.add(new BasicNameValuePair("AmountSpent", price1));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
result = line;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
protected void onPostExecute (String result)
{
if(result != null)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://152.226.152.175/NCO/WebService.asmx/UpdateParticulars");
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("NRIC", pNric));
nameValuePairs.add(new BasicNameValuePair("FixedAmount", finalBalance1));
nameValuePairs.add(new BasicNameValuePair("Name", pName));
nameValuePairs.add(new BasicNameValuePair("Class", pClass));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
答案 0 :(得分:0)
@Override is missing before onPostExecute(String result), and check result getting null
答案 1 :(得分:0)
创建一个AsyncTask实例,然后最后执行它。并且还缺少覆盖方法。
instance.execute();
并添加
@override before the post execute method.
答案 2 :(得分:0)
您需要将@Override添加到onPostExecute方法中,并且您也不能在主线程上使用网络传输。 onPostExecute中的内容需要在另一个AsyncTask中完成。
网络运营可能涉及不可预测的延迟。为了防止这种情况 从导致糟糕的用户体验,始终执行网络操作 在与UI的单独线程上。 AsyncTask类提供了一个 从UI线程启动新任务的最简单方法
答案 3 :(得分:0)
@override
protected void onPostExecute(String result){
Log.i( “AsynckTask”,结果);
}
如果您想咨询其他网络服务,您需要其他AsynkTask记住在后台进行的连接而不是UIThread
@override
protected void onPostExecute(String result){
Log.i( “AsynckTask”,结果);
//建立新连接
new AsynkTas()。execute();
}