0

I' m developing simple android app where I'm accessing servlet using this code but I'm getting java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() error. I tried to search it on google but can't find anything."MY_IP_ADDRESS" is to be considered as an Ipadress of my system that I removed. please anyone can suggest where I'm lacking in calling AsyncTask.?

package com.example.nirmal.gaminghub;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
 import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class DetailsOfGame extends AppCompatActivity {

public String gameID="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details_of_game);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    TextView tv = (TextView) findViewById(R.id.ID);
    Bundle extras = getIntent().getExtras();
        String value = extras.getString("gameID");
        //tv.setText(value);
        gameID = value;
        Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
        new ViewDetailsTask(DetailsOfGame.this).execute();
        Toast.makeText(getApplicationContext(), "hello there", Toast.LENGTH_LONG).show();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

public class ViewDetailsTask extends AsyncTask<Void, Void, Void>{
    TextView tv=(TextView)findViewById(R.id.ID);
    StringBuilder getOutput = new StringBuilder();

    DetailsOfGame detailsOfGame=null;
    ViewDetailsTask(DetailsOfGame detailsOfGame)
    {
        this.detailsOfGame=detailsOfGame;
    }
    protected Void doInBackground(Void... param) {

        try {
            String gmID=gameID;
            URL openUrl = new URL("http://MY_IP_ADDRESS:8080/GamingHub/ShowDataServlet");
            Toast.makeText(getApplicationContext(), "in async task ", Toast.LENGTH_LONG).show();

            HttpURLConnection connection = (HttpURLConnection) openUrl.openConnection();

            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = "";

            while ((line = br.readLine()) != null) {
                getOutput.append(line);
            }
            br.close();

        } catch (Exception e) {
            e.printStackTrace();
           // Toast.makeText(getApplicationContext(), "in async task exception", Toast.LENGTH_LONG).show();
        }
        return null;
    }
    protected void onPostExecute(Void result)
    {
        tv.setText(getOutput);
    }
}
}
Nirmal Purohit
  • 67
  • 1
  • 4
  • 12

2 Answers2

1

Try to call Toast.makeText(...) from your UI thread:

getActivity().runOnUiThread(new Runnable() {
  public void run() {
    Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
  }
});
motis10
  • 2,484
  • 1
  • 22
  • 46
0

Your textView will not be accessible from your AsyncTask.

Step 1 . Create a TextView variable in your Activity :

private TextView       tv;

Step 2. In the onCreate of your activity, get the corresponding textView :

tv=(TextView)findViewById(R.id.ID);

Step 3. Get rid of the declaration you have in your async task

EDIT : As G. Blake Meike mentioned, the error comes from the fact that you cannot call UI functions (such as findViewById) in a worker thread. See https://stackoverflow.com/a/3875204/4706693 Your call to Toast.makeText() in doInBackground will therefore be forbidden there too.

Community
  • 1
  • 1
NSimon
  • 5,212
  • 2
  • 22
  • 36