我想使用给定的url通过JSONParser在textview上显示值

时间:2015-04-28 12:08:37

标签: java android xml

我想显示给定JSON网址的about_builder,但我无法在JSONObject上正确声明JSONArrayMainActivity,所以请任何人帮我展示TextView上的数据。 显示错误:

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.and_ws_builder / com.example.and_ws_builder.MainActivity}:   android.os.NetworkOnMainThreadException

MainActivity.java类

 public class MainActivity extends Activity {   
        private static String url ="http://myfourwalls.in/services_microsite/project_builder.php";
        private static final String TAG_Name = "about_builder";
        JSONArray builder = null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            JSONParser jParser = new JSONParser();        
            JSONObject json = jParser.getJSONFormUrl(url);        
            try {

                builder = json.getJSONArray(TAG_Name);
                 JSONObject c = builder.getJSONObject(0);

                for (int i = 0; i < json.length(); i++) {

                    String name = c.getString(TAG_Name);                
                    final TextView nam = (TextView) findViewById(R.id.txtname);
                    nam.setText(name);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

JSONParser.java

public class JSONParser {
     static InputStream is = null;
     static JSONObject jobj = null;
     static String json = "";
    public JSONParser() {
    }

    public JSONObject getJSONFormUrl(String url) {  
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }catch (ClientProtocolException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null ){
                sb.append(line +"\n");
            }
            is.close();
            json = sb.toString();

        } catch (Exception e) {
             Log.e("Buffer Error","Error converting result" + e.toString());
        }
        try {
            jobj = new JSONObject(json);

        } catch (JSONException e) {
            Log.e("JSON Parse","error Parsing data" + e.toString());
        }
        return jobj;
    }
}

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >    
     <TextView 
         android:id="@+id/txtname"
         android:layout_width="fill_parent"
         android:layout_height="100dp"/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您在主线程中使用网络相关代码。使用AsyncTask

答案 1 :(得分:0)

你在主线程中调用网络调用意味着android中不支持前台。所以使用assynctask类来处理后台的网络调用。

1)在create方法调用内部如:

new GetJson().execute();

2)声明以下oncreate

     private class GetJson extends AsyncTask<String, Void, String> {

        JSONObject c=null;
     @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

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

    JSONParser jParser = new JSONParser();        
            JSONObject json = jParser.getJSONFormUrl(url);        
            try {

                builder = json.getJSONArray(TAG_Name);
                 c = builder.getJSONObject(0);


            } catch (JSONException e) {
                e.printStackTrace();
            }
             return c;
      }

     @Override
        protected void onPostExecute(final JSONObject result) {

                     for (int i = 0; i < result.length(); i++) {

                    String name = result.getString(TAG_Name);                
                    final TextView nam = (TextView)   findViewById(R.id.txtname);
                    nam.setText(name);
                }





}