Android android.os.NetworkOnMainThreadException Error

Asked 2 years ago, Updated 2 years ago, 75 views

I keep getting the following error.

I've already added the internet to the manifest.

<uses-permission android:name="android.permission.INTERNET" />

If you look up the data, you cannot perform networking-related tasks in the main execution thread of the app What I'm curious about is re-engineering my code in a way that Android recommends.

Below is my code.

package com.problemio;

import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        // Shows the login email form. 
        final EditText loginEmail = (EditText) findViewById(R.id.login_email);  
        String name = loginEmail.getText().toString();  

        // Show the password form. 
        final EditText password = (EditText) findViewById(R.id.password);  
        String text = password.getText().toString();                  

        // Transfer button 
        Button submit = (Button)findViewById(R.id.submit);   




        // // Show options for create-profile and forgot-password




        submit.setOnClickListener(new Button.OnClickListener() 
        {  
           public void onClick(View v) 
           {
              String email = loginEmail.getText().toString();
              String pass = password.getText().toString(); 
              sendFeedback(pass, email);
            }
        });        
    }


    public void sendFeedback(String pass , String email) 
    {  
        Log.d( "1" , pass );
        Log.d( "1" , email );

        // Go to the db and see if there's an ID like this 
        // What should I do? :)
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();  
        postParameters.add(new BasicNameValuePair("username", email ));  
        postParameters.add(new BasicNameValuePair("password", pass ));

        String responseString = null;

        try 
        {  
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("myUrl");

            I don't know what this is about :)
            httppost.setEntity(new UrlEncodedFormEntity(postParameters));

            // // This is the line that send the request
            HttpResponse response = httpclient.execute(httppost);

            HttpEntity entity = response.getEntity();            

            InputStream is = entity.getContent();
        } 
        catch (Exception e) 
        {     
            Log.e("log_tag", "Error in http connection "+e.toString());
        }        
    }          
}

What's wrong? Please fix it!

android networkonmainthread

2022-09-22 14:07

1 Answers

This exception is an error that occurs when trying to network on the main thread.

Calling the sendfeedback method to asynctask will work well. Usually, the operation of requesting a webserver takes a lot of time, so the main thread becomes unresponsive while waiting, so you have to return it to another thread to avoid it. Therefore, asyncTask is a good choice.

http://android-developers.blogspot.in/2009/05/painless-threading.html If you go here, you'll find information about asynctask.


2022-09-22 14:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.