Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:52:19+00:00 2026-06-11T20:52:19+00:00

I am a new in android and I am trying to do some async

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:52:20+00:00Added an answer on June 11, 2026 at 8:52 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am completely new to Android and currently trying to generate listview from JSON
I'm trying to share some text using an intent: Intent i = new Intent(android.content.Intent.ACTION_SEND);
im fairly new to android development, but i'm having some trouble trying to make
I'm trying just some very simple code to get an Android widget going but
I'm new to android developing and I'm trying to open a popup window from
I am a new android developer trying to code a widget.. The updatePeriodMillis parameter
I'm pretty new to Android development and would appreciate some help. All I want
Hello StackOverflow Users, I am new to android and trying to develop a game
I am new to android..Actually iam trying to increase the RAM Memory of android
I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.