0

Im trying to create an app that gets a json object from a url. This is proving to be unnecessarily frustrating as it keeps crashing on the activity that is supposed to load and parse the json object. It just pops up the message "Unfortunately, (AppName) has stopped." and then exits the application. The data from the JSON is never shown on the screen. Here is the code with the activity and the JSON parsing

package com.example.Accomplist;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: DESAI_628IL
 * Date: 3/1/13
 * Time: 7:34 PM
 * To change this template use File | Settings | File Templates.
 */
public class MainScreen extends Activity{
    TextView myTextView;
   // HttpClient client;
    // url to make request
    final static String url = "http://accomplist.herokuapp.com/api/v1/sharedevent/2/?format=json";

    private static final String TAG_EVENT="event"; //A JSON object within the JSON object that will be returned by JSONParse()
    private static final String TAG_DESCRIPTION="description"; //A JSON tag within the JSON object EVENT
    private static String eventString="Yo";
    static JSONObject json;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);
        new JSONParse().execute(url);
    }
private class JSONParse extends AsyncTask<String, String, String> {
    HttpClient client;
    JSONObject jsonObj= null;
    @Override
    protected String doInBackground(String... jsonurl) {
        StringBuilder url= new StringBuilder(String.valueOf(jsonurl));
        HttpGet get= new HttpGet(url.toString());
        HttpResponse r= null;
        try {
            r = client.execute(get);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        int status= r.getStatusLine().getStatusCode();
        if (status==200){
            HttpEntity e=r.getEntity();
            String data= null;
            try {
                data = EntityUtils.toString(e);
            } catch (IOException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            try {
                jsonObj = new JSONObject(data);
            } catch (JSONException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            try {
                JSONObject eventJson= jsonObj.getJSONObject(TAG_EVENT);
                eventString= eventJson.getString(TAG_DESCRIPTION);
            }
            catch (JSONException e1) {
                eventString="Couldn't Parse Data";
            }
            return eventString;
        }
        else{
            return eventString;
        }
    }
    protected void onProgressUpdate(String... progress) {
        Toast loadingToast= Toast.makeText(getApplicationContext(), "Loading", Toast.LENGTH_LONG);
        loadingToast.show();
    }
    protected void onPostExecute(String result) {
        eventString=result;
        myTextView = (TextView)findViewById(R.id.textView1);
        myTextView.setText(eventString);
    }
}
}

I hope someone can help. Ive been stuck on this for the longest time. Ive tried many things. Ive tried different ways of getting the JSON object from the url, putting the parsing code in a different class, and lots of other small things. Any help would be appreciated. Here is the LogCat error report

02-26 12:18:46.691: ERROR/ThrottleService(324): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
02-26 12:19:53.601: WARN/dalvikvm(4073): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:19:55.121: WARN/dalvikvm(4073): threadid=12: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:19:57.632: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4073 uid 10048
02-26 12:20:03.452: WARN/dalvikvm(4094): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:20:04.744: ERROR/AndroidRuntime(4094): FATAL EXCEPTION: AsyncTask #1
        java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
        at java.util.concurrent.FutureTask.run(FutureTask.java:239)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
        Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 0: [Ljava.lang.String;@40d12070
        at java.net.URI.create(URI.java:727)
        at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
        at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:84)
        at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:78)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        ... 4 more
02-26 12:20:10.512: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4094 uid 10048
02-26 12:20:24.401: WARN/dalvikvm(4111): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:20:24.441: ERROR/AndroidRuntime(4111): FATAL EXCEPTION: AsyncTask #1
        java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
        at java.util.concurrent.FutureTask.run(FutureTask.java:239)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
        Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 0: [Ljava.lang.String;@40d12038
        at java.net.URI.create(URI.java:727)
        at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
        at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:84)
        at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:78)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        ... 4 more
02-26 12:20:29.411: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4111 uid 10048
sourdesi
  • 360
  • 1
  • 4
  • 21

6 Answers6

0

Try with this

new JSONParse().execute("");

instead of

new JSONParse().execute(url);

can u paste your logcat message so that i can help u more accurately.

Rohit
  • 490
  • 3
  • 15
  • I just tried new JSONParse().execute(""); and I still got the same Unfortunately, (AppName) has stopped notification. And I have posted the LogCat error – sourdesi Mar 07 '13 at 06:10
0

try something like this..

HttpClient client = new DefaultHttpClient();
        // Perform a GET request for a JSON list
        HttpUriRequest request = new HttpGet("https://somejson.json");
        // Get the response that sends back
        HttpResponse response = null;
        try {
            response = client.execute(request);
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
Janmejoy
  • 2,721
  • 1
  • 20
  • 38
  • This worked perfectly!! You effectively solved a problem that has given me a weeks worth of headaches. Thank you so much!! – sourdesi Mar 07 '13 at 06:18
0

The parameter in the doInbackgroud() is work like an array so that you can pass multiple argument to this method while calling the execute method on AsyncTask. The first passed parameter would be stored on the 0th position and so on. Since you are only passing one parameter to the execute method, that parameter would be stored on the 0th position... So you need to get the URL from jsonurl[0]

Try with

StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0]));
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
0

Change your doInBackground method as :

  @Override
    protected String doInBackground(String... jsonurl) {
     StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0]));
      HttpGet get= new HttpGet(url.toString());
      HttpResponse r= null;

currently you are passing wrong url to get data form web service. see following to known move how we get value from varargs inside doInBackground method

What does the "..." mean in a parameter list? doInBackground(String... params)

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

You Haven't get your Http client

set

HttpClient httpClient = new DefaultHttpClient(); in your doInBackGround()

Pragnani
  • 20,075
  • 6
  • 49
  • 74
0

You have 2 mistakes

1. StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0])); 
  // jsonurl[0]

2. client = new DefaultHttpClient();                                
   // Add this line too
Kabir
  • 852
  • 7
  • 11
Naveen
  • 1,703
  • 13
  • 22