2012-11-01 41 views
1

我有這段代碼,它返回Json對象從URL。 它在froyo中正常工作,但相同的代碼在Jelly Beans中不起作用。代碼不工作在果凍豆

我的代碼有問題嗎?

請幫助我,這裏是我的代碼

public static JSONObject getJSONfromURL(String url) { 

    // initialize 
    InputStream is = null; 
    String result = ""; 
    JSONObject jArray = null; 

    // http post 
    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(url); 
     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); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     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()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jArray = new JSONObject(result); 
    } catch (JSONException e) { 
     Log.e("log_tag", "Error parsing data " + e.toString()); 
    } 

    return jArray; 
} 

這裏是

11-01 11:01:48.437: E/log_tag(1044): Error in http connection android.os.NetworkOnMainThreadException 11-01 11:01:48.437: E/log_tag(1044): Error converting result java.lang.NullPointerException 11-01 11:01:48.437: E/log_tag(1044): Error parsing data org.json.JSONException: End of input at character 0 of

+0

去沒有任何錯誤?如果是的話請張貼。是的,你正在做網絡相關的任務,所以請確保你沒有在主線程中執行此操作請參閱[this](http://android-developers.blogspot.in/2009/05/painless-threading.html) – juned

+0

意思是「不工作」? –

+0

究竟是什麼問題只看到logcat是否有錯誤? – Ravi

回答

1

從Android API v15開始,主線程無需繁重的工作。所以,你應該你的邏輯轉移到像波紋管的源代碼另一個線程:

new Thread(new Runnable() { 
    public void run() { 
     // your logic 
    }       
}).start(); 

更多信息,請參閱 http://developer.android.com/guide/practices/responsiveness.html

+0

你是對的Chirag Patel,需要在單獨的線程中分割任務 – Antarix

+0

@Antarix right.i認爲你的問題solve.any查詢? – ckpatel

+0

是的,它解決了我的問題:)你能告訴我如何更新UI?我在日誌中獲得價值。 – Antarix

1

你正在試圖建立在主線程上的網絡連接我的日誌錯誤。請使用AsyncTask。 因爲網絡任務是很長的任務。在froyo中,你可以在主線程上建立網絡,但在Android的更高版本中,你必須使用Async Task

1

在後臺線程中進行服務器調用。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    new AsyncTask<Void,Void,Void>() { 

     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      JSONObject jObject = getJSONfromURL("http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%2248907%22&format=json"); 
      return null; 
     } 
    }.execute(); 
} 
public JSONObject getJSONfromURL(String url) { 

    // initialize 
    InputStream is = null; 
    String result = ""; 
    JSONObject jArray = null; 

    // http post 
    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(url); 
     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); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     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()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jArray = new JSONObject(result); 
    } catch (JSONException e) { 
     Log.e("log_tag", "Error parsing data " + e.toString()); 
    } 

    return jArray; 
} 
1

呀其誤差約android.os.NetworkOnMainThreadException 使用AsyncTask做網絡相關的任務。

以另一種方式,你可以使用你的onCreate()

if (android.os.Build.VERSION.SDK_INT > 9) { 
StrictMode.ThreadPolicy policy = 
     new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy); 
} 

這段代碼禁用此但見禁用StrictMode政策是不是一個好主意,讓你更好地與AsyncTask