JSon没有获取数据

时间:2015-07-22 06:09:02

标签: java android json eclipse httprequest

我在这里包含了我的全部代码。由于某种原因,它不会进入类JsonHandler,因此我无法获取所需的数据。我似乎无法理解这里的问题。可能真的很傻。

我已经使用toast的帮助检查了它是否没有进入Json Handler类。

public class MainActivity extends Activity implements OnClickListener {

Button prem,work,tshirt,tech,ntech,query,reg,about;
String[] name,org,desc,date,time,loc,cname1,cname2,pho1,pho2,email,team,fee;
int k;
int[] tid,ftid;
String[] fname,forg,fdesc,fdate,ftime,floc,fcname1,fcname2,fpho1,fpho2,femail,fteam,ffee;

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

    new request().execute();



    prem=(Button) findViewById (R.id.button1);
    about=(Button) findViewById (R.id.babout);
    tshirt=(Button) findViewById (R.id.btshirt);
    work=(Button) findViewById (R.id.button4);
    tech=(Button) findViewById (R.id.button2);
    ntech=(Button) findViewById (R.id.button5);
    reg=(Button) findViewById (R.id.button3);
    query=(Button) findViewById (R.id.button6);
    prem.setOnClickListener(this);
    about.setOnClickListener(this);
    tshirt.setOnClickListener(this);
    work.setOnClickListener(this);
    tech.setOnClickListener(this);
    ntech.setOnClickListener(this);
    reg.setOnClickListener(this);
    query.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

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

    switch(v.getId()){

    case R.id.babout:



    break;

    case R.id.btshirt:

        break;

    case R.id.button1:


        break;

    case R.id.button2:

        break;

    case R.id.button3:

        break;

    case R.id.button4:

        break;

    case R.id.button5:


        break;

    case R.id.button6:

        break;

    }
    //Events o = null;
    //o.set(ftid, fname, fdate, ffee, fcname1, fcname2, fpho1, fpho2, floc, forg, fdesc, femail, fteam, ftime);
}


private boolean isOnline() {
    // TODO Auto-generated method stub
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netinfo = cm.getActiveNetworkInfo();
    return netinfo!=null && netinfo.isConnected();
}

private class request extends AsyncTask<Void, Void, List<String>>{

    private ProgressDialog dialog;
    AndroidHttpClient client= AndroidHttpClient.newInstance("");
    HttpGet request;
    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

    @Override
    protected List<String> doInBackground(Void... arg0) {
        if (isOnline())
        {
            try{
                Toast.makeText(getApplicationContext(), "Start", 
                           Toast.LENGTH_LONG).show();
                request = new HttpGet("www.com");

            }catch(NullPointerException e ){
                e.printStackTrace();}
            jsonhandler handler = new jsonhandler();
            try
            {
                return client.execute(request,handler);
            }catch(ClientProtocolException exception){
                exception.printStackTrace();
            }catch(IOException exception){
                exception.printStackTrace();
            }

        }
        return null;
    }
    protected void onPostExecute(List<String> result)
      {
        super.onPostExecute(result);
        if (this.client != null) {
          this.client.close();
        }
      }

}
private class jsonhandler implements ResponseHandler<List<String>>{
@Override
public List<String> handleResponse(HttpResponse response)
        throws ClientProtocolException, IOException {

    //List<String> result = new ArrayList<String>();
    String jsonresponse = new BasicResponseHandler().handleResponse(response);
    int i;
    JSONArray data;
    try {
        Toast.makeText(getApplicationContext(), "Json Enter", 
                   Toast.LENGTH_LONG).show();
        data = new JSONArray(jsonresponse);
        for(i=0;i<data.length();i++)
        {
            JSONObject obj = (JSONObject)data.get(i);
            name[i]=obj.get("name").toString();
            org[i]=obj.get("organisation").toString();
            desc[i]=obj.getString("description").toString();
            date[i]=obj.get("date").toString();
            time[i]=obj.get("timings").toString();
            loc[i]=obj.get("location").toString();
            cname1[i]=obj.get("cname1").toString();
            pho1[i]=obj.get("cno1").toString();
            cname2[i]=obj.get("cname2").toString();
            pho2[i]=obj.get("cno2").toString();
            email[i]=obj.get("email").toString();
            team[i]=obj.get("team").toString();
            tid[i]=Integer.parseInt(obj.get("tid").toString());
            fee[i]=obj.get("fee").toString();


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

    return null;
}

}
} 

错误是

java.lang.RuntimeException: An error occured while executing doInBackground()

at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:327)
at android.widget.Toast.<init>(Toast.java:92)
at android.widget.Toast.makeText(Toast.java:241)
at com.example.gravitas2015.MainActivity$jsonhandler.handleResponse(MainActivity.java:269)
at com.example.gravitas2015.MainActivity$jsonhandler.handleResponse(MainActivity.java:1)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:273)
at com.example.gravitas2015.MainActivity$request.doInBackground(MainActivity.java:240)
at com.example.gravitas2015.MainActivity$request.doInBackground(MainActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)

1 个答案:

答案 0 :(得分:0)

您无法在后台线程中显示Toast消息。删除下面的代码,然后重试。

Toast.makeText(getApplicationContext(), "Json Enter", 
               Toast.LENGTH_LONG).show();