2013-02-19 162 views
0

請幫我找到這段代碼中的錯誤。 我想從化JSON遠程服務器(PHP & MySQL的)一些數據,然後解析它 問題是結果回報,但它應該是這個鏈接返回http://codeincloud.tk/json_android_example.phpjson解析錯誤

package com.shadatv.shada; 

import java.io.IOException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

public class canticlesActivity extends Activity { 

    TextView httpStuff; 
    HttpClient client; 
    JSONObject json; 

    final static String URL = "http://codeincloud.tk/json_android_example.php"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.canticles); 

     httpStuff = (TextView) findViewById(R.id.textView1); 
     client = new DefaultHttpClient(); 
     new Read().execute("name"); 
    } 

    public JSONObject lastTweet() throws ClientProtocolException, IOException, JSONException { 
     StringBuilder url = new StringBuilder(URL); 

     HttpGet get = new HttpGet(url.toString()); 
     HttpResponse r = client.execute(get); 
     int status = r.getStatusLine().getStatusCode(); 

     if (status == 200) { 
      HttpEntity e = r.getEntity(); 
      String data = EntityUtils.toString(e); 
      JSONArray timeline = new JSONArray(data); 
      JSONObject last = timeline.getJSONObject(0); 
      return last; 

     } else { 
      Toast.makeText(getBaseContext(), "error", Toast.LENGTH_SHORT); 
      return null; 
     } 
    } 

    public class Read extends AsyncTask<String, Integer, String>{ 

     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      try { 
       json = lastTweet(); 
       return json.getString(params[0]); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getBaseContext(), "ahmed .. " + result, Toast.LENGTH_LONG).show(); 
      httpStuff.setText(result); 
     } 

    } 
} 
+1

你可以粘貼一些像StackStraces的LogCat輸出嗎? – Droidman 2013-02-19 11:07:18

+0

你爲什麼試圖解析JSONArray中的Json對象? – njzk2 2013-02-19 11:11:03

回答

0

複製並過去現在它正在工作。 我改變了JSONObject last = new JSONObject(data);

因爲http://codeincloud.tk/json_android_example.php重新調整的JSONObject的

package com.shadatv.shada; 

import java.io.IOException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

public class canticlesActivity extends Activity { 

    TextView httpStuff; 
    HttpClient client; 
    JSONObject json; 

    final static String URL = "http://codeincloud.tk/json_android_example.php"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.canticles); 

     httpStuff = (TextView) findViewById(R.id.textView1); 
     client = new DefaultHttpClient(); 
     new Read().execute("name"); 
    } 

    public JSONObject lastTweet() throws ClientProtocolException, IOException, JSONException { 
     StringBuilder url = new StringBuilder(URL); 

     HttpGet get = new HttpGet(url.toString()); 
     HttpResponse r = client.execute(get); 
     int status = r.getStatusLine().getStatusCode(); 

     if (status == 200) { 
      HttpEntity e = r.getEntity(); 
      String data = EntityUtils.toString(e); 

      JSONObject last = new JSONObject(data); 
      return last; 

     } else { 
      Toast.makeText(getBaseContext(), "error", Toast.LENGTH_SHORT); 
      return null; 
     } 
    } 

    public class Read extends AsyncTask<String, Integer, String>{ 

     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      try { 
       json = lastTweet(); 
       return json.getString(params[0]); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getBaseContext(), "ahmed .. " + result, Toast.LENGTH_LONG).show(); 
      httpStuff.setText(result); 
     } 

    } 
} 
+0

謝謝,它現在正在工作 – 2013-02-19 12:36:23

+0

還有另一個問題,如果我想要接收像這樣的json數組,我該怎麼辦:http://dt-works.com/ags/shadatv/canticles/android_data 怎麼可以我修改它? – 2013-02-19 12:37:27

1

解析當前JSON字符串爲:

HttpEntity e = r.getEntity(); 
String data = EntityUtils.toString(e); 
// convert data to json object 
JSONObject last =new JSONObject(data); 

因爲當前webservic e僅重新調整JSONObject而不是JSONArray

1

您的網址提供了以下數據:

{ 「名」:即 「Froyo」, 「版本」: 「Android 2.2的」}

的代碼解析JSON的塊如下:

 JSONArray timeline = new JSONArray(data); 
     JSONObject last = timeline.getJSONObject(0); 

這使得您正在閱讀的JSON文檔是一個數組而不是一個對象。 (或者是有效的JSON,但是你提取的文檔很明顯是一個Object)。 Web服務調用可能並不總是返回一個或另一個,所以你必須小心,你正在解析正確的類型。