我想在php服务器上传递最后一个id。我最后插入了id但是在发送到服务器时出现“主线程异常网络”的问题。
public void get_data(String data) {
try {
JSONArray data_array = new JSONArray(data);
for (int i = 0; i < data_array.length(); i++) {
JSONObject obj = new JSONObject(data_array.get(i).toString());
Message add = new Message();
Log.d("JSONVALUE :", obj.toString());
String category = obj.optString("category");
String status = obj.optString("status");
String imgLink = obj.optString("imagelink");
String id = obj.optString("id");
add.setCategory(category);
add.setStatus(status);
add.setImagelink(imgLink);
add.setId(id);
objlist.add(add);
dataBaseHelper.addSMS(category, status, imgLink);
if(i==data_array.length()-1){
lastId=id;
Log.d("LAstID","LastId"+lastId);
new SendLastID().execute();
}
}
//new ListAdapter(CatagoryActivity.this, objlist).notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
class SendLastID extends AsyncTask<String,Void,Void> {
private Exception exception;
@Override
protected Void doInBackground(String... urls) {
// TODO Auto-generated method stub
//send last id to php server
HttpClient httpclient = new DefaultHttpClient();
//String string2 = "B";
HttpPost httppost = new HttpPost("askmanu.in/demo.php?id="+lastId);
//HttpResponse response = (HttpResponse) httpclient.execute(httppost);
try {
// Execute HTTP Post Request
HttpResponse response = (HttpResponse) httpclient.execute(httppost);
//int status = response.getStatusLine().getStatusCode();
//System.out.println("data posted, status = " + status);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}
Logcat - java.lang.RuntimeException:执行doInBackground()时发生错误
05-11 15:14:39.933: E/AndroidRuntime(23721): FATAL EXCEPTION: AsyncTask #4
05-11 15:14:39.933: E/AndroidRuntime(23721): Process: marathi.sms.collection, PID: 23721
05-11 15:14:39.933: E/AndroidRuntime(23721): java.lang.RuntimeException: An error occured while executing doInBackground()
05-11 15:14:39.933: E/AndroidRuntime(23721): at android.os.AsyncTask$3.done(AsyncTask.java:304)
05-11 15:14:39.933: E/AndroidRuntime(23721): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-11 15:14:39.933: E/AndroidRuntime(23721): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-11 15:14:39.933: E/AndroidRuntime(23721): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-11 15:14:39.933: E/AndroidRuntime(23721): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-11 15:14:39.933: E/AndroidRuntime(23721): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-11 15:14:39.933: E/AndroidRuntime(23721): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-11 15:14:39.933: E/AndroidRuntime(23721): at java.lang.Thread.run(Thread.java:818)
05-11 15:14:39.933: E/AndroidRuntime(23721): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=askmanu.in/demo.php
05-11 15:14:39.933: E/AndroidRuntime(23721): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:624)
05-11 15:14:39.933: E/AndroidRuntime(23721): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:312)
05-11 15:14:39.933: E/AndroidRuntime(23721): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:580)
05-11 15:14:39.933: E/AndroidRuntime(23721): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:503)
05-11 15:14:39.933: E/AndroidRuntime(23721): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:481)
05-11 15:14:39.933: E/AndroidRuntime(23721): at marathi.sms.collection.CatagoryActivity$SendLastID.doInBackground(CatagoryActivity.java:202)
05-11 15:14:39.933: E/AndroidRuntime(23721): at marathi.sms.collection.CatagoryActivity$SendLastID.doInBackground(CatagoryActivity.java:1)
05-11 15:14:39.933: E/AndroidRuntime(23721): at android.os.AsyncTask$2.call(AsyncTask.java:292)
05-11 15:14:39.933: E/AndroidRuntime(23721): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-11 15:14:39.933: E/AndroidRuntime(23721): ... 4 more
答案 0 :(得分:1)
class SendLastID extends AsyncTask<Void, Void, Void> {
private Exception exception;
protected Void doInBackground(Void... urls) {
if(i==data_array.length()-1){
String lastId=id;
Log.d("LAstID","LastId"+lastId);
//send last id to php server
HttpClient httpclient = new DefaultHttpClient();
//String string2 = "B";
HttpPost httppost = new HttpPost("http://askmanu.in/demo.php?id=" + lastId);
//HttpResponse response = (HttpResponse) httpclient.execute(httppost);
try {
// Execute HTTP Post Request
HttpResponse response = (HttpResponse) httpclient.execute(httppost);
//int status = response.getStatusLine().getStatusCode();
//System.out.println("data posted, status = " + status);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
protected void onPostExecute(Void aVoid ) {
// TODO: check this.exception
// TODO: do something with the feed
}
}
以
运行上述课程new SendLastID().execute(); //you ca
答案 1 :(得分:0)
使用Asynck任务对于呼叫WebApi
#The basic use case
my $client = REST::Client->new();
$client->GET('http://example.com/dir/file.xml');
print $client->responseContent();
#A host can be set for convienience
$client->setHost('http://example.com');
$client->PUT('/dir/file.xml', '<example>new content</example>');
if( $client->responseCode() eq '200' ){
print "Deleted\n";
}
#custom request headers may be added
$client->addHeader('CustomHeader', 'Value');
#response headers may be gathered
print $client->responseHeader('ResponseHeader');
#X509 client authentication
$client->setCert('/path/to/ssl.crt');
$client->setKey('/path/to/ssl.key');
#add a CA to verify server certificates
$client->setCa('/path/to/ca.file');
#you may set a timeout on requests, in seconds
$client->setTimeout(10);
#options may be passed as well as set
$client = REST::Client->new({
host => 'https://example.com',
cert => '/path/to/ssl.crt',
key => '/path/to/ssl.key',
ca => '/path/to/ca.file',
timeout => 10,
});
$client->GET('/dir/file', {CustomHeader => 'Value'});
#Requests can be specificed directly as well
$client->request('GET', '/dir/file', 'request body content', {CustomHeader => 'Value'});
//运行Asynck任务
private class WebApiAsynck extends AsyncTask<Void,Void,Void>{
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this)
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
if(i==data_array.length()-1){
String lastId=id;
Log.d("LAstID", "LastId" + lastId);
//send last id to php server
HttpClient httpclient = new DefaultHttpClient();
//String string2 = "B";
HttpPost httppost = new HttpPost("http://askmanu.in/demo.php?id=" + lastId);
//HttpResponse response = (HttpResponse) httpclient.execute(httppost);
try {
// Execute HTTP Post Request
HttpResponse response = (HttpResponse) httpclient.execute(httppost);
//int status = response.getStatusLine().getStatusCode();
//System.out.println("data posted, status = " + status);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.cancel();
progressDialog.dismiss();
}
}