Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:asynctask中json解析出错_Android_Json - Fatal编程技术网

Android:asynctask中json解析出错

Android:asynctask中json解析出错,android,json,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

我试图使用异步任务解析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());



}
公共类FetchData扩展异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
//TODO自动生成的方法存根
JSONParser=新的JSONParser();
JSONObject object=parser.getJSONFromUrl(“http://en.wikipedia.org/w/api.php?action=parse&format=json&page=India&prop=sections");
试一试{
JSONObject pbj1=object.getJSONObject(“解析”);
JSONArray user=pbj1.getJSONArray(“节”);
值1=新的ArrayList();
//anc=新数组列表();

对于(int i=0;它不需要线程调用asynctask.,我已经在没有线程的情况下完成了。它仍然显示相同的错误@Raghunandan