0

代码

private static final String TAG_SUCCESS = "success";

String URL_SERVER = "http://localhost/italk/servlet/login.ValidateUser";
//String URL_SERVER = "http://localhost/italk/servlet/login.ValidateUser";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    inputUserName = (EditText) findViewById(R.id.txtUserName);
    inputPassWord = (EditText) findViewById(R.id.txtPassWord);

    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Neu login ->true
            new doLogin().execute();
        }   
    });
}

class doLogin extends AsyncTask<String, String, String> {

    /**
    * Getting product details in background thread
    * */
    protected String doInBackground(String... params) {

        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
        public void run() {
            // Check for success tag
            String success;
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("user_name", inputUserName.getText().toString()));
                params.add(new BasicNameValuePair("pass_word", inputPassWord.getText().toString()));
                params.add(new BasicNameValuePair("db_name", "jdbc/ITTALKDS"));
                // getting product details by making HTTP request
                // Note that product details url will use GET request
                JSONObject json = jsonParser.makeHttpRequest(
                URL_SERVER, "GET", params);

                // check your log for json response
                //Log.d("Single Product Details", json.toString());

                // json success tag
                success = json.getString("result");
                if (success.equals("2")) {//Login successfully
                    pDialog = new ProgressDialog(MainActivity.this);
                    pDialog.setMessage("Login successfully...");
                    Intent i = new Intent(getApplicationContext(), AlliTalk.class);
                    startActivity(i);
                } else {
                    pDialog = new ProgressDialog(MainActivity.this);
                    pDialog.setMessage("Login failt...");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        });

        return null;
    }
}

错误信息:

06-24 23:17:01.946: E/AndroidRuntime(7968): FATAL EXCEPTION: main
06-24 23:17:01.946: E/AndroidRuntime(7968): android.os.NetworkOnMainThreadException
06-24 23:17:01.946: E/AndroidRuntime(7968):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at libcore.io.IoBridge.connect(IoBridge.java:112)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at java.net.Socket.connect(Socket.java:842)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at com.italk.JSONParser.makeHttpRequest(JSONParser.java:64)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at com.italk.MainActivity$doLogin$1.run(MainActivity.java:74)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at android.os.Handler.handleCallback(Handler.java:725)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at android.os.Looper.loop(Looper.java:137)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at java.lang.reflect.Method.invokeNative(Native Method)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at java.lang.reflect.Method.invoke(Method.java:511)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-24 23:17:01.946: E/AndroidRuntime(7968):     at dalvik.system.NativeStart.main(Native Method)
4

4 回答 4

1

您不能在 UI 线程上进行网络调用。将您的runOnUiThread()函数移动到您想要进行 UI 线程操作的位置。

于 2013-06-24T16:45:11.087 回答
0

删除runOnUIThread()并直接在 中提出请求doInBackground()。中的任何代码doInBackground()都在单独的线程上运行,因此您不需要在那里运行。

于 2013-06-24T16:37:29.950 回答
0

只需删除runOnUiThread后台线程内部。因为 doInBanckground 已经使用单独的线程运行以在后台执行该过程

于 2013-06-24T16:39:24.617 回答
0

这个问题已经在 SO 上提出了很多次,并且每次都得到了解决。

看文章AsyncTask

编辑:起初我没有看到你的代码。你不应该在runOnUIThread. 堆栈跟踪状态“主线程上的网络”。这意味着您应该在AsyncTask'sdoInBackground()方法中执行网络部分,并从主线程更新 UI。尝试在 处获取值AsyncTask并将其返回到主线程,然后更新 UI。查看之前链接的文章以获得清晰的想法。

于 2013-06-24T16:32:43.753 回答