0

Code

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

error message:

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)
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • That's because you cannot use network services on main thread. Use `AsyncTask` to accomplish this... – Writwick Jun 24 '13 at 16:29

4 Answers4

1

You cannot make network call on UI thread. Move your runOnUiThread() function to where you want to make a UI Thread operation.

tasomaniac
  • 10,234
  • 6
  • 52
  • 84
0

This question has been made on SO quite several times and has been solved every time.

Look at the article AsyncTask

EDIT : I didn't see your code at first. You should not make httprequests in the runOnUIThread. The stacktrace states "Network on main thread". Which means you should do the network part in AsyncTask's doInBackground() method, and update UI from main thread. Try getting the values at the AsyncTask and returning it to the main thread then update UI. Look at the article linked before for a clear idea.

Writwick
  • 2,133
  • 6
  • 23
  • 54
0

Remove the runOnUIThread() and directly make the request in doInBackground(). Any code in doInBackground() is run on a separate thread so you don't need a runnable there.

Mus
  • 1,860
  • 2
  • 16
  • 19
0

just remove the runOnUiThread inside the background thread. Because doInBanckground already run with separate thread to do the process in background

Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • runOnUiThread is needed as parts of that code interact with the UI. What needs to be done is to use it *only* for those parts, and not for the whole thing including the network code. – Chris Stratton Jun 24 '13 at 16:49