-1

i have created a jsonobject that pulls data from a server but it works only on android versions less than 3 (old ones). what will be the code to make it work on all the versions??

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DefaultHttpClient client;
    client = new DefaultHttpClient();
    HttpGet get = new HttpGet("http://saurabhgoyal.comoj.com/json_test.php");
    HttpResponse r;
    try {
        r = client.execute(get);
        HttpEntity e = r.getEntity();
        InputStream is=e.getContent();
        InputStreamReader isr=new InputStreamReader(is);
        BufferedReader reader=new BufferedReader(isr);
        String results=reader.readLine();
        Toast.makeText(this, results, Toast.LENGTH_SHORT).show();
    } catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "failure", Toast.LENGTH_SHORT).show();
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

3 Answers3

1

You shouldn't do any network calls on the UI thread you need to use a separate thread or an AsyncTask to do your network call in.

In versions below 3.0 you could get away with doing that, but in 3.0 you get a NetworkOnMainThreadException when you do that.

cdbitesky
  • 1,390
  • 1
  • 13
  • 30
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • ohk .. can u guide me how can i resolve it more elaborately ?? – Saurabh Goyal Sep 24 '13 at 17:56
  • what is the error you are getting?and i guess you are talking about supporting the app for various versions then check your manifest file the minimum and target sdk version you have set.Also use an asynctask in your code. – khubaib Sep 24 '13 at 17:57
  • I already told you, you need to start a new `Thread` or use an `AsyncTask` many examples can be easily found by searching this site or even the android docs – tyczj Sep 24 '13 at 17:57
1

Looks like your running into NetworkOnMainThreadException. You could try to set Strict Mode off using:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

However, I would advise you should move any long running operations, or network operations into a background thread, like an AsyncTask.

Here you go with the Async task

  protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
   new LongOperation().execute();
}

private class LongOperation extends AsyncTask<Void, Void, Void> {
        String results;
      @Override
      protected void doInBackground(String... params) {
                DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://saurabhgoyal.comoj.com/json_test.php");
HttpResponse r;
try {
    r = client.execute(get);
    HttpEntity e = r.getEntity();
    InputStream is=e.getContent();
    InputStreamReader isr=new InputStreamReader(is);
    BufferedReader reader=new BufferedReader(isr);
    results=reader.readLine();

} catch (ClientProtocolException e1) {
    // TODO Auto-generated catch block
    Toast.makeText(this, "failure", Toast.LENGTH_SHORT).show();
    e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
            return "Executed";
      }      

      @Override
      protected void onPostExecute(String result) {
          Toast.makeText(this, results, Toast.LENGTH_SHORT).show();
      }


   }   

Hope that solves your issue.

S.A.Norton Stanley
  • 1,833
  • 3
  • 23
  • 37
0

your answer is hereJson From Url

hope it helps.

if you prefer to save it and then process it... you can use:

try {
        InputStream is = getAssets().open("drillData.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        String str = new String(buffer, "UTF-8");
        JSONArray arr = new JSONArray(str);

        for (int i = 0; i < arr.length(); i++) {
            list.add(arr.getJSONObject(i));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

for example json array from assets

Community
  • 1
  • 1
Jony-Y
  • 1,579
  • 1
  • 13
  • 30