0

I have written the following program to read a json file from server and display its contents in android. But its not working and showing a long list of errors. Somebody please guide me and tell where I went wrong. I am new to android programming.

package com.example.json_display;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter; 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends ListActivity {

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,this.populate())); 
    }

    private ArrayList<String> populate() {
        ArrayList<String> items = new ArrayList<String>();

        try {
            URL url = new URL("http://www.pooja8bhatt.tk/info.php");
            HttpURLConnection urlConnection =(HttpURLConnection)url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

            String next;
            while ((next = bufferedReader.readLine()) != null){
                JSONArray ja = new JSONArray(next);
                System.out.println(next);

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    items.add(jo.getString("text"));
                    //System.out.println("inside");
                }
            }
        } catch (MalformedURLException 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 items;
    }
}

logcat is:

09-09 18:59:42.777: E/Trace(763): error opening trace file: No such file or directory (2)
09-09 18:59:43.298: D/AndroidRuntime(763): Shutting down VM
09-09 18:59:43.298: W/dalvikvm(763): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-09 18:59:43.327: E/AndroidRuntime(763): FATAL EXCEPTION: main
09-09 18:59:43.327: E/AndroidRuntime(763): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.json_display/com.example.json_display.MainActivity}: android.os.NetworkOnMainThreadException
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.os.Looper.loop(Looper.java:137)
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-09 18:59:43.327: E/AndroidRuntime(763):  at java.lang.reflect.Method.invokeNative(Native Method)
09-09 18:59:43.327: E/AndroidRuntime(763):  at java.lang.reflect.Method.invoke(Method.java:511)
09-09 18:59:43.327: E/AndroidRuntime(763):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-09 18:59:43.327: E/AndroidRuntime(763):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-09 18:59:43.327: E/AndroidRuntime(763):  at dalvik.system.NativeStart.main(Native Method)
09-09 18:59:43.327: E/AndroidRuntime(763): Caused by: android.os.NetworkOnMainThreadException
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-09 18:59:43.327: E/AndroidRuntime(763):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-09 18:59:43.327: E/AndroidRuntime(763):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-09 18:59:43.327: E/AndroidRuntime(763):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-09 18:59:43.327: E/AndroidRuntime(763):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
09-09 18:59:43.327: E/AndroidRuntime(763):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-09 18:59:43.327: E/AndroidRuntime(763):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
09-09 18:59:43.327: E/AndroidRuntime(763):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
09-09 18:59:43.327: E/AndroidRuntime(763):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-09 18:59:43.327: E/AndroidRuntime(763):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
09-09 18:59:43.327: E/AndroidRuntime(763):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
09-09 18:59:43.327: E/AndroidRuntime(763):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
09-09 18:59:43.327: E/AndroidRuntime(763):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
09-09 18:59:43.327: E/AndroidRuntime(763):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
09-09 18:59:43.327: E/AndroidRuntime(763):  at com.example.json_display.MainActivity.populate(MainActivity.java:35)
09-09 18:59:43.327: E/AndroidRuntime(763):  at com.example.json_display.MainActivity.onCreate(MainActivity.java:25)
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.app.Activity.performCreate(Activity.java:5008)
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-09 18:59:43.327: E/AndroidRuntime(763):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-09 18:59:43.327: E/AndroidRuntime(763):  ... 11 more
09-09 18:59:45.997: I/Process(763): Sending signal. PID: 763 SIG: 9

I have added the oncreate method in this code which I forgot earlier.

4

0 回答 0