Android:在asynctask中进行json解析时出错

时间:2014-02-14 07:50:47

标签: android json

我正在尝试使用异步任务解析json,但它显示错误“进程在主线程中执行太多工作”我的代码就在这里..

JSONParser.java -

public class JSONParser  {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
    // Making HTTP request
    try {
        // defaultHttpClient
        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 parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    // return JSON String
    return jObj;
}

}

和MainActivity.java是 -

public class FetchData extends AsyncTask<Void,Void,Void>{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        JSONParser parser=new JSONParser();
         JSONObject object=parser.getJSONFromUrl("http://en.wikipedia.org/w/api.php?action=parse&format=json&page=India&prop=sections");
         try {
                JSONObject pbj1=object.getJSONObject("parse");
            JSONArray   user=pbj1.getJSONArray("sections");
                 values1 = new ArrayList<String>();
                // anc = new ArrayList<String>();

                for(int i=0;i<user.length();i++){
                    JSONObject ib=user.getJSONObject(i);
                     lines=ib.getString(TAG_LINE);
                    // anch=ib.getString(TAG_ANCHOR);
                     toclevel=ib.getInt("toclevel");
                    // anchor=ib.getString(TAG_ANCHOR);
                    if(lines!=null&&toclevel==1){

                        values1.add(lines);
                       // anc.add(anch);


                    }


                }

         }
         catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();



            }
        return null;    
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub

        super.onPostExecute(result);
        bt.setText(values1.get(0));
         bt1.setText(values1.get(1));

         bt2.setText(values1.get(2));
         bt3.setText(values1.get(3));
         bt4.setText(values1.get(4));
         bt5.setText(values1.get(5));
          bt6.setText(values1.get(6));
          bt7.setText(values1.get(7));
          bt8.setText(values1.get(8));


    }

}

public class A implements OnClickListener {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}


    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity1);

    read();

    //ArrayList<String> bd=getIntent().getStringArrayListExtra("arraylist");
    Thread thread=new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            new FetchData().execute();

        }
    });
    thread.start();


    //Intent i=getIntent();
    //values=i.getStringArrayListExtra("list");
    //a=i.getStringExtra("X");
}

private void read() {
    // TODO Auto-generated method stub
    bt=(Button)findViewById(R.id.his);

    //bt.setOnClickListener(new A());
     bt.setOnClickListener(new A());
     bt1=(Button)findViewById(R.id.bio);
             bt1.setOnClickListener(new A());
     bt2=(Button)findViewById(R.id.ety);

      bt2.setOnClickListener(new A());
     bt3=(Button)findViewById(R.id.pol);

      bt3.setOnClickListener(new A());
     bt4=(Button)findViewById(R.id.eco);

      bt4.setOnClickListener(new A());
     bt5=(Button)findViewById(R.id.geo);

      bt5.setOnClickListener(new A());
     bt6=(Button)findViewById(R.id.cul);
      bt6.setOnClickListener(new A());
     bt7=(Button)findViewById(R.id.mil);

      bt7.setOnClickListener(new A());
     bt8=(Button)findViewById(R.id.mor);
      bt8.setOnClickListener(new A());



}

}  我只想从维基百科中检索印度的内容值表并将其设置在按钮文本上。

0 个答案:

没有答案