2015-07-22 96 views
0

我在這裏包含了我的整個代碼。出於某種原因,它不會進入JsonHandler類,因此我無法獲取所需的數據。我似乎無法理解這裏的問題。可能真的很愚蠢。JSon沒有獲取數據

我已經通過使用乾杯的幫助檢查過它沒有進入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) 
+0

有沒有錯誤?堆棧跟蹤 ?你正在使用的網址?處理程序調用之上有一個catch塊...你之前是否有錯誤? – saurav

+0

沒有錯誤。只是我存儲日期的字符串仍然是空的 –

+0

對不起。我確實有一個錯誤 現在會更新 –

回答

0

不能在後臺線程顯示敬酒消息。刪除下面的代碼並重試。

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

仍然無法獲取數據。當我嘗試打印名字時崩潰[0] 獲取NPE。必須表示我從來沒有收到任何數據嗎? –