class XXX implements Runnable
{
String lat,lon,str,taluka_name;
int name;
HttpResponse response;
HttpEntity entity;
InputStream is = null;
Toast s1;
StringBuilder sb=null;
TextView v;
Spinner s;
public String result[];
TextView tv;
LinearLayout ll1;
int i;
ArrayList<Integer> croplist;
public XXX(String t_n,String [] res,LinearLayout ll,TextView tv1)
{
croplist= new ArrayList<Integer>();
taluka_name = t_n;
result = res;
ll1= ll;
tv = tv1;
}
@Override
public void run() {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost("http://10.0.2.2/proagri115.php");
List<NameValuePair>login=new ArrayList<NameValuePair>();
login.add(new BasicNameValuePair("location", taluka_name));
try
{
UrlEncodedFormEntity entity=new UrlEncodedFormEntity(login);
request.setEntity(entity);
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
response = httpclient.execute(request);
entity = response.getEntity();
is = entity.getContent();
System.out.println("Executed the request");
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.printStackTrace();
System.out.println("");
}
catch(Exception e)
{
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
str=sb.toString();
Log.e("log_tag", "Success converting result "+sb.toString());
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
System.out.println(str+"I have executed");
result = str.split(">>");
System.out.println("length"+result.length);
for(i=0;i<result.length;i++)
{
System.out.println("\n"+i+"=="+result[i]);
}
System.out.println("Notified");
}
}
}
public class help extends Activity{
int j;
Intent i;
String s,taluka_name;
EditText edt,edt1,edt2;
Double lat,lon;
Spinner spin;
String [] re;
TextView tv;
Layout lt;
LinearLayout lt1;
XXX runnable;
public void onCreate(Bundle savedinstancestate)
{
super.onCreate(savedinstancestate);
setContentView(R.layout.help);
lt1 = (LinearLayout)findViewById(R.id.ll1);
s =(String)getIntent().getSerializableExtra("Rainfall");
taluka_name =(String)getIntent().getSerializableExtra("location");
System.out.println(s);
tv = new TextView(this);
tv.setText("Crops for Your Selected Area are");
lt1.addView(tv);
try
{
runnable = new XXX(taluka_name,re,lt1,tv);
Thread threadX = new Thread(runnable);
System.out.println("till this");
threadX.start();
System.out.println("In Waited");
try
{
wait(500);
}
catch (IllegalMonitorStateException e)
{
System.out.println("IllegalMonitorStateException");
}
catch (InterruptedException e)
{
System.out.println("InterruptedException");
}
System.out.println("Out of Waited");
}
catch(Exception e)
{
System.out.println("Error again "+e);
}
try{
System.out.println("Final Result will be");
for(j=0;j<runnable.result.length;j++)
{
tv = new TextView(this);
tv.setText(runnable.result[j]);
System.out.println(runnable.result[j]);
lt1.addView(tv);
}
}
catch(Exception e)
{
}
}
}
I have main activity and thread XXX. I want to use the result of httprequest in XXX thread to be used in Main activity.But before XXX completes its operation main thread executes and
I get NullpointerException . How should I use network response in main activity . I have tried “synchronized block” . But It works for methods of single class.
How should I solve this problem?
At minimum, you need to use Thread.join to wait for the thread to complete.. But that would block on your UI thread which is really bad. You should really just do this the android way and use an AsyncTask
Read this:
http://developer.android.com/resources/articles/painless-threading.html
Here is what you should do:
Then from your activity call:
Now the tricky thing is you need to get that result back to your UI thread.. The easiest way is to put the AsyncTask within the activity as an inner class, then in onPostExecute of the asyncTask you just call some function from your activity.
If you want the AsyncTask in a seperate file then you need to pass a reference of your class to the constructor of the AsyncTask and then you can call any public method of your activity from the asyncTask. Just remember that you can only call the activities methods in onPostExecute and in onProgressUpdate (do not call UI methods in doInBackground)