2013-07-16 98 views
0

我試圖從url中獲取json數據。我的數據如下:無法從url中獲取json數據android

[{"Food1":"Fried Chicken","Food2":"Spagetti","Food3":"Watermelon"}] 

我已經搜索了它,幾乎嘗試了我可以找到的每個代碼。但他們沒有爲我工作。 當我手動給出一個json數據我可以很容易地解析它,真正的問題是通過使用HttpClient或Httpurlconnection從URL獲取數據。我也嘗試過使用AsyncTask,但是我無法正確地做到這一點。我該怎麼辦 ?

的logcat:https://dl.dropboxusercontent.com/u/108584907/logcat.txt

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

public class yemekhane extends Activity { 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.yemekhane); 

    TextView tv = (TextView) findViewById(R.id.TextView01); 


    JSONArray jArray = null; 
    String result = ""; 
    StringBuilder sb = null; 
    InputStream is = null; 

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 

     //Why to use 10.0.2.2 
     HttpPost httppost = new HttpPost("http://example.com"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
     }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection"+e.toString()); 
     } 


    //convert response to string 
    try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      sb = new StringBuilder(); 
      sb.append(reader.readLine() + "\n"); 

      String line="0"; 
      while ((line = reader.readLine()) != null) { 
          sb.append(line + "\n"); 
      } 
      is.close(); 
      result=sb.toString(); 


      }catch(Exception e){ 
        Log.e("log_tag", "Error converting result "+e.toString()); 
      } 


    String name = ""; 

      try { 
      jArray = new JSONArray(result); 
       JSONObject json_data=null; 
       for(int i=0;i<jArray.length();i++){ 
        json_data = jArray.getJSONObject(i); 
        name=json_data.getString("Food1"); 
       } 
     } catch (JSONException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
      tv.setText("test " + name); 


    } 


}` 
+0

慈祥發佈logcat的PLZ – KOTIOS

+0

發表關於你做了什麼一些代碼。只是在這裏發佈JSON不會幫助。 – rockstar

+0

你確定你得到的迴應是你的想法嗎?在解析之前嘗試打印它。 – Steelight

回答

0

感謝您的意見,我終於找到了我的答案。 首先,我在Android Manifest xml文件中將minsdkversion更改爲9。然後我說這一點下面的代碼:

StrictMode.ThreadPolicy policy = new StrictMode. 
ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy); 

它的工作對我來說,希望工程爲別人也:)