我在2个不同的活动中使用2个AsyncTask将数据发布到我的android app中的不同URL。但是只有第一个AsyncTask的数据被发布到服务器。但是第二个AsyncTask数据被存储到namevalue对因为我可以在我的日志中看到它,但没有被发布到服务器。可以帮助解决这个问题吗?
提前致谢。
这是我的第一个AsyncTask
private class PostData extends AsyncTask<String, String, HttpResponse> {
protected HttpResponse doInBackground(String... params) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://application.easypani.com/app/customer/register");
HttpResponse response = null;
try {
// Add your data
Name = full_name.getText().toString();
Phonenumber = phone.getText().toString();
Email = email.getText().toString();
Password = password.getText().toString();
House = house.getText().toString();
Street = street.getText().toString();
Landmark = landmark.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", Name));
nameValuePairs.add(new BasicNameValuePair("phone", Phonenumber));
nameValuePairs.add(new BasicNameValuePair("email", Email));
nameValuePairs.add(new BasicNameValuePair("password", Password));
nameValuePairs.add(new BasicNameValuePair("house", House));
nameValuePairs.add(new BasicNameValuePair("area", Area));
nameValuePairs.add(new BasicNameValuePair("street", Street));
nameValuePairs.add(new BasicNameValuePair("landmark", Landmark));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.e("post_", nameValuePairs.toString());
// Execute HTTP Post Request
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return response;
}
}
这是我在另一项活动中的第二个AyncTask
private class PostOrder extends AsyncTask<String, Void, HttpResponse> {
protected HttpResponse doInBackground(String... params) {
// Create a new HttpClient and Post Header
HttpClient httpclient1 = new DefaultHttpClient();
HttpPost httppost1 = new HttpPost("http://application.easypani.com/app/customer/order");
HttpResponse response1 = null;
try {
// Add your data
List<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>();
nameValuePairs1.add(new BasicNameValuePair("suppliername", SupplierName));
nameValuePairs1.add(new BasicNameValuePair("quantity", Quantity));
nameValuePairs1.add(new BasicNameValuePair("newdispenser", dispenserchecked));
nameValuePairs1.add(new BasicNameValuePair("newconnection", newconnectionchecked));
httppost1.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
Log.e("post_order", nameValuePairs1.toString());
// Execute HTTP Post Request
response1 = httpclient1.execute(httppost1);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return response1;
}
}
答案 0 :(得分:1)
不是在每个活动中创建AsyncTask,而是创建一个单独的AsyncTask类,可以从任何活动或片段中调用它。
在这里,我为AsyncTask编写了一个单独的类,响应将通过接口到达活动。
Click here to get the samples from drive
在您的活动或片段中实现接口,并覆盖该方法以从您的活动或片段中获取服务器的响应。
由于SDK 23中不再支持HttpClient,您需要在libs文件夹中导入 org.apache.http.legacy.jar (包含在驱动器中)文件。
但是,您可以使用 Google排球,而不是使用AsyncTask。
排球提供
Volley Android Official documentation
Introduction to Volley
Volley Example Code