我正在使用HTTPPOST方法更新android应用程序中的数据库,但出现以下错误
11-1013:32:47.398:E/AndroidRuntime(2155):致命异常:main 11-10 13:32:47.398:E/AndroidRuntime(2155):android.os.NetworkOnMainThreadException 11-10 13:32:47.398:E/AndroidRuntime(2155):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 11-10 13:32:47.398:E/AndroidRuntime(2155):位于java.net.InetAddress.lookupHostByName(InetAddress.java:385) 11-10 13:32:47.398:E/AndroidRuntime(2155):位于java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 11-10 13:32:47.398:E/AndroidRuntime(2155):位于java.net.InetAddress.getAllByName(InetAddress.java:214) 公共类更新数据库扩展异步任务时出错我正在使用HTTPPOST方法更新android应用程序中的数据库,但出现以下错误,android,http-post,Android,Http Post,11-1013:32:47.398:E/AndroidRuntime(2155):致命异常:main 11-10 13:32:47.398:E/AndroidRuntime(2155):android.os.NetworkOnMainThreadException 11-10 13:32:47.398:E/AndroidRuntime(2155):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:11
package com.nikhil.svs;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
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.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Register extends MainActivity {
EditText reg,pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
reg=(EditText) findViewById(R.id.editusername);
pwd=(EditText) findViewById(R.id.editpassword);
Button register=(Button) findViewById(R.id.register);
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new UpdatingDatabase().execute();
}
public class UpdatingDatabase extends AsyncTask<String, String, String>
{
protected String doInBackground(String... arg0) { //add this line
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://nikhilvaddepati.tk.hostinghood.com/test.php");
String a=reg.getText().toString();
String b=pwd.getText().toString();
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("regno", ""+a));
nameValuePairs.add(new BasicNameValuePair("pwd",""+b));
httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
public void execute() {
// TODO Auto-generated method stub
}
}// and this brackets
});
}
}
package com.nikhil.svs;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
公共类注册扩展活动{
编辑文本注册表,pwd;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
reg=(EditText)findViewById(R.id.editusername);
pwd=(EditText)findViewById(R.id.editpassword);
按钮寄存器=(按钮)findviewbyd(R.id.register);
register.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
新建更新数据库().execute();
}
公共类更新数据库扩展异步任务
{
受保护的字符串doInBackground(字符串…arg0){//添加此行
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://nikhilvaddepati.tk.hostinghood.com/test.php");
字符串a=reg.getText().toString();
字符串b=pwd.getText().toString();
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“regno”、“a”);
添加(新的BasicNameValuePair(“pwd”)和“+b”);
setEntity((HttpEntity)新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
public void execute(){
//TODO自动生成的方法存根
}
}//这个括号
});
}
}
正如我在评论中所说的,您需要使用线程来获取网络,并确保不在UI线程上执行任何网络访问,而是在异步任务中执行
您必须将onClick按钮中的代码移动到AsynTask
它应该如下所示
编辑
public void onClick(View v) {
new UpdatingDatabase().execute();
}
public class UpdatingDatabase extends AsyncTask<String, String, String>
{
protected String doInBackground(String... arg0) { //add this line
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://nikhilvaddepati.tk.hostinghood.com/test.php");
String a=reg.getText().toString();
String b=pwd.getText().toString();
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("regno", ""+a));
nameValuePairs.add(new BasicNameValuePair("pwd",""+b));
httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}// and this brackets
}
编辑2:
package com.nikhil.svs;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
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.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Register extends MainActivity {
EditText reg,pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
reg=(EditText) findViewById(R.id.editusername);
pwd=(EditText) findViewById(R.id.editpassword);
Button register=(Button) findViewById(R.id.register);
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new UpdatingDatabase().execute();
}
});
}
public class UpdatingDatabase extends AsyncTask<String, String, String>
{
protected String doInBackground(String... arg0) { //add this line
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://nikhilvaddepati.tk.hostinghood.com/test.php");
String a=reg.getText().toString();
String b=pwd.getText().toString();
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("regno", ""+a));
nameValuePairs.add(new BasicNameValuePair("pwd",""+b));
httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
package com.nikhil.svs;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
公共类注册扩展活动{
编辑文本注册表,pwd;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
reg=(EditText)findViewById(R.id.editusername);
pwd=(EditText)findViewById(R.id.editpassword);
按钮寄存器=(按钮)findviewbyd(R.id.register);
register.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
新建更新数据库().execute();
}
});
}
公共类更新数据库扩展异步任务
{
受保护的字符串doInBackground(字符串…arg0){//添加此行
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://nikhilvaddepati.tk.hostinghood.com/test.php");
字符串a=reg.getText().toString();
字符串b=pwd.getText().toString();
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
package com.nikhil.svs;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
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.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Register extends MainActivity {
EditText reg,pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
reg=(EditText) findViewById(R.id.editusername);
pwd=(EditText) findViewById(R.id.editpassword);
Button register=(Button) findViewById(R.id.register);
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new UpdatingDatabase().execute();
}
});
}
public class UpdatingDatabase extends AsyncTask<String, String, String>
{
protected String doInBackground(String... arg0) { //add this line
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://nikhilvaddepati.tk.hostinghood.com/test.php");
String a=reg.getText().toString();
String b=pwd.getText().toString();
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("regno", ""+a));
nameValuePairs.add(new BasicNameValuePair("pwd",""+b));
httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}