首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在使用http方法更新android应用程序中的数据库,但我得到了以下错误

我正在使用http方法更新android应用程序中的数据库,但我得到了以下错误
EN

Stack Overflow用户
提问于 2013-11-10 05:50:11
回答 1查看 1K关注 0票数 0

11-10 13:32:47.398: E/AndroidRuntime(2155):致命例外:主11-10 13:32:47.398: E/AndroidRuntime(2155):android.os.NetworkOnMainThreadException 11-10 13:32:47.398: E/AndroidRuntime(2155):at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 11-10 13:32:47.398: E/AndroidRuntime(2155):at 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):at java.net.InetAddress.getAllByName(InetAddress.java:214)

公共类UpdatingDatabase扩展AsyncTask时出错

代码语言:javascript
复制
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
                        });
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-10 06:06:23

正如我在注释中指出的,您需要使用线程来获取网络,并确保不对UI线程执行任何网络访问,而是在异步任务中进行访问。

您必须将onClick按钮中的代码移动到AsynTask..。

应该如下所示。

编辑!

代码语言:javascript
复制
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
}

现在它应该是这样的,它将修复您的错误。如果成功了,请告诉我

编辑:

代码语言:javascript
复制
final EditText reg, pwd;

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);

    ...// complete with the code

编辑2:

代码语言:javascript
复制
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
                        }

                    }

                  }
                 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19886704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档