0

I am a new in android and I am trying to do some async task with Json and I want to reach the datas from tke Json file. And my code is:

package com.example.httpsample;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.httpsample.HttpExample.Read;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    TextView httpsetup;
    HttpClient client;
    JSONObject json;

    //url tanımlama:
    final static String URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.httpex);
        httpsetup = (TextView) findViewById(R.id.tvHttp);
        //client açıyoruz.
        client = new DefaultHttpClient();
        //hangi datayı almak istiyorsak onun keyini alıyoruz
        new Read().execute("text");
    }

    public JSONObject lastTweet(String username) throws ClientProtocolException, IOException, JSONException{

        //call url
        StringBuilder url = new StringBuilder(URL);

        url.append(username);

        //url ye request gönderme
        HttpGet get = new HttpGet(url.toString());
        //response from url
        HttpResponse r = client.execute(get);

        int status = r.getStatusLine().getStatusCode();
        if (status == 200){

            HttpEntity e = r.getEntity();
            String data = EntityUtils.toString(e);
            JSONArray timeline = new JSONArray(data);
            JSONObject last = timeline.getJSONObject(0);
            return last;
        }else{
            Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
            return null;
        }
    }

    public class Read extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... params) {

            // TODO Auto-generated method stub
        try {
            json = lastTweet("mybringback");
            return json.getString(params[0]);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            // TODO Auto-generated method stub
            httpsetup.setText(result);
        }
    }

}

But when I run the project I face the Force close problem and my eclipse says that:

- 09-26 07:17:17.964: E/AndroidRuntime(275): FATAL EXCEPTION: AsyncTask
   #1 09-26 07:17:17.964: E/AndroidRuntime(275): java.lang.RuntimeException: An error occured while executing
   doInBackground() 09-26 07:17:17.964: E/AndroidRuntime(275):  at
   android.os.AsyncTask$3.done(AsyncTask.java:200) 09-26 07:17:17.964:
   E/AndroidRuntime(275):   at
   java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
   09-26 07:17:17.964: E/AndroidRuntime(275):   at
   java.util.concurrent.FutureTask.setException(FutureTask.java:124)
   09-26 07:17:17.964: E/AndroidRuntime(275):   at
   java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
   09-26 07:17:17.964: E/AndroidRuntime(275):   at
   java.util.concurrent.FutureTask.run(FutureTask.java:137) 09-26
   07:17:17.964: E/AndroidRuntime(275):     at
   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
   09-26 07:17:17.964: E/AndroidRuntime(275):   at
   java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
   09-26 07:17:17.964: E/AndroidRuntime(275):   at
   java.lang.Thread.run(Thread.java:1096) 09-26 07:17:17.964:
   E/AndroidRuntime(275): Caused by: java.lang.RuntimeException: Can't
   create handler inside thread that has not called Looper.prepare()
   09-26 07:17:17.964: E/AndroidRuntime(275):   at
   android.os.Handler.<init>(Handler.java:121) 09-26 07:17:17.964:
   E/AndroidRuntime(275):   at android.widget.Toast.<init>(Toast.java:68)
   09-26 07:17:17.964: E/AndroidRuntime(275):   at
   android.widget.Toast.makeText(Toast.java:231) 09-26 07:17:17.964:
   E/AndroidRuntime(275):   at
   com.example.httpsample.MainActivity.lastTweet(MainActivity.java:67)
   09-26 07:17:17.964: E/AndroidRuntime(275):   at
   com.example.httpsample.MainActivity$Read.doInBackground(MainActivity.java:79)
   09-26 07:17:17.964: E/AndroidRuntime(275):   at
   com.example.httpsample.MainActivity$Read.doInBackground(MainActivity.java:1)
   09-26 07:17:17.964: E/AndroidRuntime(275):   at
   android.os.AsyncTask$2.call(AsyncTask.java:185) 09-26 07:17:17.964:
   E/AndroidRuntime(275):   at
   java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
   09-26 07:17:17.964: E/AndroidRuntime(275):   ... 4 more 09-26
   07:17:18.184: D/dalvikvm(275): GC_FOR_MALLOC freed 2898 objects /
   203488 bytes in 211ms 09-26 07:22:18.404: I/Process(275): Sending
   signal. PID: 275 SIG: 9

errors.

Can anyone helps me?

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Çağatay Aktaş
  • 97
  • 1
  • 4
  • 10

2 Answers2

5

Remove the line

 Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();

It is not allowed to access gui elemts from a trhead. You can show a Toast inside the asynctask class by overriding onPreExecute or onPostExecute, but not in doInBackground

Also there is something wrong with your Webservice, because the status code isn't 200 :-)

Thommy
  • 5,070
  • 2
  • 28
  • 51
1

The error is the line:

Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();

you should remove it from the method lastTweet as you cannot call a UI method from doInBackground.. add it in the onPostExecute instead..

protected void onPostExecute(String result) {

        if(result == null)
            Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
        else
            httpsetup.setText(result);
    }
Nermeen
  • 15,883
  • 5
  • 59
  • 72