-1

I've searched the other similar questions but none of it works for me so I want to ask you guys.

  02-07 18:39:03.953 6252-6306/com.example.jl.httpsample
       E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
  Process: com.example.jl.httpsample, PID: 6252
  java.lang.RuntimeException: An error occured while executing doInBackground()
  at android.os.AsyncTask$3.done(AsyncTask.java:300)
  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at java.lang.Thread.run(Thread.java:841)
  Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  at android.os.Handler.<init>(Handler.java:200)
  at android.os.Handler.<init>(Handler.java:114)
  at android.widget.Toast$TN.<init>(Toast.java:372)
  at android.widget.Toast.<init>(Toast.java:105)
  at android.widget.Toast.makeText(Toast.java:264)
  at com.example.jl.httpsample.MainActivity$1GetJSON.doInBackground(MainActivity.java:70)
  at com.example.jl.httpsample.MainActivity$1GetJSON.doInBackground(MainActivity.java:45)
  at android.os.AsyncTask$2.call(AsyncTask.java:288)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at java.lang.Thread.run(Thread.java:841) 

Here's my Code


public class MainActivity extends AppCompatActivity {
    private String json;
    private  String[] data;
    public static final String MY_JSON = "MY_JSON";
    private static final String JSON_URL = "http://127.0.0.1/httpsample.php";
    private  EditText txtuser, txtpass;
    Button btnlogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtpass = (EditText) findViewById(R.id.txtUsername);
        txtuser = (EditText) findViewById(R.id.txtPassword);
        btnlogin = (Button) findViewById(R.id.btnLogin);
        try {
            btnlogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getJSON(JSON_URL);
                }
            });
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
        }


    }
    private void getJSON(String url){
        class GetJSON extends AsyncTask<String,Void, String> {
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(MainActivity.this,"Please wait...",null,true,true);
            }
            @Override
            protected String doInBackground(String... params) {
                String uri = params[0];

                BufferedReader bufferedReader;
                try{
                    URL url = new URL(uri);
                    HttpURLConnection urlConnection =  (HttpURLConnection) url.openConnection(); //opens connection duh jl
                    StringBuilder stringBuilder = new StringBuilder();
                    bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

                    while((json = bufferedReader.readLine())!=null){
                       data = json.split(" ");
                    }

                    return data.toString();
                }
                catch (Exception e){
                    Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
                    return null;
                }

            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                txtuser.setText(s);
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute(url);
    }
}

It was mention that the error came from doInBackground() but im new to Android development so it's hard for me to understand what is happening.

Anupam
  • 3,702
  • 18
  • 54
  • 87
  • 1
    post the code that includes your implementation of `AsyncTask` as well! – OBX Feb 07 '17 at 11:14
  • `Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();` throwing the exception its ui elemnt you are running in doinbackground method – Abhishek Singh Feb 07 '17 at 11:19

2 Answers2

0

The problem is with

catch (Exception e){          
    Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
    return null;
 }

Toast needs to run on UI thread which violates the policy as doInBackgroun runs in worker thread. Handle the error case in onPostExecute.

Nayan Srivastava
  • 3,515
  • 3
  • 26
  • 48
0

Please do use Jsonparser file to run such doInBackground methods. Please find the link below for further knowledge about JsonParser class.

How to implement Login with HttpURLConnection and PHP server in Android

Community
  • 1
  • 1
Ismaran Duwadi
  • 1,541
  • 12
  • 10