2013-03-07 116 views
0

我試圖創建一個從url獲取json對象的應用程序。 這證明是不必要的令人沮喪,因爲它不斷崩潰的應該加載和解析json對象的活動。它只是彈出消息「不幸的是,(AppName)已停止。」然後退出應用程序。來自JSON的數據從不顯示在屏幕上。這裏是與活動和JSON解析代碼崩潰JSON解析Android應用程序

package com.example.Accomplist; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.RemoteException; 
import android.widget.TextView; 
import android.widget.Toast; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 

/** 
* Created with IntelliJ IDEA. 
* User: DESAI_628IL 
* Date: 3/1/13 
* Time: 7:34 PM 
* To change this template use File | Settings | File Templates. 
*/ 
public class MainScreen extends Activity{ 
    TextView myTextView; 
    // HttpClient client; 
    // url to make request 
    final static String url = "http://accomplist.herokuapp.com/api/v1/sharedevent/2/?format=json"; 

    private static final String TAG_EVENT="event"; //A JSON object within the JSON object that will be returned by JSONParse() 
    private static final String TAG_DESCRIPTION="description"; //A JSON tag within the JSON object EVENT 
    private static String eventString="Yo"; 
    static JSONObject json; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_screen); 
     new JSONParse().execute(url); 
    } 
private class JSONParse extends AsyncTask<String, String, String> { 
    HttpClient client; 
    JSONObject jsonObj= null; 
    @Override 
    protected String doInBackground(String... jsonurl) { 
     StringBuilder url= new StringBuilder(String.valueOf(jsonurl)); 
     HttpGet get= new HttpGet(url.toString()); 
     HttpResponse r= null; 
     try { 
      r = client.execute(get); 
     } catch (IOException e) { 
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
     } 
     int status= r.getStatusLine().getStatusCode(); 
     if (status==200){ 
      HttpEntity e=r.getEntity(); 
      String data= null; 
      try { 
       data = EntityUtils.toString(e); 
      } catch (IOException e1) { 
       e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
      } 
      try { 
       jsonObj = new JSONObject(data); 
      } catch (JSONException e1) { 
       e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
      } 
      try { 
       JSONObject eventJson= jsonObj.getJSONObject(TAG_EVENT); 
       eventString= eventJson.getString(TAG_DESCRIPTION); 
      } 
      catch (JSONException e1) { 
       eventString="Couldn't Parse Data"; 
      } 
      return eventString; 
     } 
     else{ 
      return eventString; 
     } 
    } 
    protected void onProgressUpdate(String... progress) { 
     Toast loadingToast= Toast.makeText(getApplicationContext(), "Loading", Toast.LENGTH_LONG); 
     loadingToast.show(); 
    } 
    protected void onPostExecute(String result) { 
     eventString=result; 
     myTextView = (TextView)findViewById(R.id.textView1); 
     myTextView.setText(eventString); 
    } 
} 
} 

我希望有人可以提供幫助。我一直堅持這個最長的時間。我曾嘗試過很多事情。我嘗試了不同的方式從url獲取JSON對象,將解析代碼放在不同的類中,還有很多其他小東西。任何幫助,將不勝感激。這裏是logcat的錯誤報告

02-26 12:18:46.691: ERROR/ThrottleService(324): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory) 
02-26 12:19:53.601: WARN/dalvikvm(4073): threadid=11: thread exiting with uncaught exception (group=0x40a71930) 
02-26 12:19:55.121: WARN/dalvikvm(4073): threadid=12: thread exiting with uncaught exception (group=0x40a71930) 
02-26 12:19:57.632: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4073 uid 10048 
02-26 12:20:03.452: WARN/dalvikvm(4094): threadid=11: thread exiting with uncaught exception (group=0x40a71930) 
02-26 12:20:04.744: ERROR/AndroidRuntime(4094): FATAL EXCEPTION: AsyncTask #1 
     java.lang.RuntimeException: An error occured while executing doInBackground() 
     at android.os.AsyncTask$3.done(AsyncTask.java:299) 
     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
     at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
     at java.lang.Thread.run(Thread.java:856) 
     Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 0: [Ljava.lang.String;@40d12070 
     at java.net.URI.create(URI.java:727) 
     at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75) 
     at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:84) 
     at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:78) 
     at android.os.AsyncTask$2.call(AsyncTask.java:287) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
     ... 4 more 
02-26 12:20:10.512: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4094 uid 10048 
02-26 12:20:24.401: WARN/dalvikvm(4111): threadid=11: thread exiting with uncaught exception (group=0x40a71930) 
02-26 12:20:24.441: ERROR/AndroidRuntime(4111): FATAL EXCEPTION: AsyncTask #1 
     java.lang.RuntimeException: An error occured while executing doInBackground() 
     at android.os.AsyncTask$3.done(AsyncTask.java:299) 
     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
     at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
     at java.lang.Thread.run(Thread.java:856) 
     Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 0: [Ljava.lang.String;@40d12038 
     at java.net.URI.create(URI.java:727) 
     at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75) 
     at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:84) 
     at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:78) 
     at android.os.AsyncTask$2.call(AsyncTask.java:287) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
     ... 4 more 
02-26 12:20:29.411: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4111 uid 10048 
+4

當它顯示'不幸的是,(AppName)已經停止'它會在'LOGCAT'中放入很多紅線。你能在這裏粘貼嗎? – SudoRahul 2013-03-07 05:56:43

+0

發佈logcat錯誤。 – rajeshwaran 2013-03-07 05:56:47

+0

您可能應該探索[logcat](http://stackoverflow.com/tags/logcat/info)並將您的'logcat'轉儲與您的問題一起發佈。 – PCoder 2013-03-07 05:57:48

回答

0

嘗試這樣的事情..

HttpClient client = new DefaultHttpClient(); 
     // Perform a GET request for a JSON list 
     HttpUriRequest request = new HttpGet("https://somejson.json"); 
     // Get the response that sends back 
     HttpResponse response = null; 
     try { 
      response = client.execute(request); 
     } catch (ClientProtocolException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
+0

這工作完美!你有效地解決了一個讓我頭疼幾周的問題。非常感謝!! – sourdesi 2013-03-07 06:18:24

+0

@sourdesi fine..enjoy !!! – Janmejoy 2013-03-07 06:19:25

0

嘗試用這種

new JSONParse().execute(""); 

代替

new JSONParse().execute(url); 

ü可以粘貼logcat的消息,這樣我可以幫助ü更準確。

+0

我剛剛嘗試了新的JSONParse()。execute(「」);而且我仍然有同樣的情況不幸的是,(AppName)已經停止通知。我發佈了LogCat錯誤 – sourdesi 2013-03-07 06:10:34

0

參數在doInbackgroud()就像是一個數組的工作,讓你可以通過多個參數來這種方法,同時調用execute方法上AsyncTask。第一個傳遞的參數將存儲在0th位置等。由於您只傳遞一個參數來執行方法,該參數將被存儲在0th位置......所以,你需要從jsonurl[0]

獲取URL與

StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0])); 
0

嘗試更改doInBackground方法作爲:

@Override 
    protected String doInBackground(String... jsonurl) { 
    StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0])); 
     HttpGet get= new HttpGet(url.toString()); 
     HttpResponse r= null; 

當前你通過錯誤的URL來獲取數據表單Web服務。看看下面的已知的舉動,我們如何從varargs獲得doInBackground方法中值

What does the "..." mean in a parameter list? doInBackground(String... params)

0

你沒有得到你的HTTP客戶端

設置

HttpClient httpClient = new DefaultHttpClient();在doInBackGround()

0

你有2次失誤

  1. StringBuilder的URL =新的StringBuilder(將String.valueOf(jsonurl [0])); // jsonurl [0]
  2. client = new DefaultHttpClient(); //也添加這一行