我試圖創建一個程序,允許用戶使用下面的代碼發送HTTP POST到PHP腳本。當我點擊發送按鈕時,我得到一個android.os.NetworkOnMainThreadException。 (btnSend)。Android - LogCat給出了一個android.os.NetworkOnMainThreadException
package com.naveed.post;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating HTTP client
final HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
final HttpPost httpPost = new HttpPost("http://10.0.2.2/android_post/test.php");
Button send = (Button)findViewById(R.id.btnSend);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText message = (EditText)findViewById(R.id.textMessage);
String msg = message.getText().toString();
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("message", msg));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
}
});
}
}
我知道我必須使用AsyncTask,所以我試過了。但我知道我做錯了。有什麼建議麼。我的代碼如下:
package com.naveed.post;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button)findViewById(R.id.btnSend);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
EditText message = (EditText)findViewById(R.id.textMessage);
String msg = message.getText().toString();
nameValuePair.add(new BasicNameValuePair("message", msg));
}
});
}
class doThePost extends AsyncTask<Void, Void, Void>{
protected Void doInBackground(ArrayList<NameValuePair>... nameValuePair) {
// TODO Auto-generated method stub
ArrayList<NameValuePair> nvPairs = nameValuePair[0];
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost("http://10.0.2.2/android_post/test.php");
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvPairs));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("hello", "done");
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
Log.d("hello", "done");
return null;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
}
你能提供更多的信息嗎?像你在哪裏得到例外等 – meilke
基本上,當我點擊發送按鈕,應用程序關閉,我得到在logcat上的follwing消息: threadid = 1:線程退出與未捕獲的異常 然後,我得到異常。 – nos4a2
我知道我必須使用AsyncTask,所以我嘗試過。但我知道我做錯了。有什麼建議麼。 – nos4a2