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