2013-09-24 99 views
-1

我已經創建了一個jsonobject,從服務器拉數據,但它只適用於小於3(舊版本)的Android版本。什麼代碼才能在所有版本上運行?使應用程序適用於所有Android版本

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    DefaultHttpClient client; 
    client = new DefaultHttpClient(); 
    HttpGet get = new HttpGet("http://saurabhgoyal.comoj.com/json_test.php"); 
    HttpResponse r; 
    try { 
     r = client.execute(get); 
     HttpEntity e = r.getEntity(); 
     InputStream is=e.getContent(); 
     InputStreamReader isr=new InputStreamReader(is); 
     BufferedReader reader=new BufferedReader(isr); 
     String results=reader.readLine(); 
     Toast.makeText(this, results, Toast.LENGTH_SHORT).show(); 
    } catch (ClientProtocolException e1) { 
     // TODO Auto-generated catch block 
     Toast.makeText(this, "failure", Toast.LENGTH_SHORT).show(); 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
+0

例如JSON數組能否請你別說當你在運行3+到底發生了什麼版本?任何錯誤味精或日誌將有所幫助 –

+0

我在我的單元格中嘗試它直接崩潰說不幸它已停止工作。 –

+0

請在應用程序崩潰時打開Logcat並查找錯誤日誌,並在可能的情況下將其粘貼到此處。 –

回答

1

你不應該做的,你需要使用一個單獨的線程UI線程或AsyncTask上的任何網絡電話做你的網絡呼叫英寸

在低於3.0的版本,你可以用做逃脫但是在3.0版本中,當你這樣做時,你會得到一個NetworkOnMainThreadException

+0

ohk ..你能指導我如何更精細地解決它? –

+0

你得到的錯誤是什麼?我猜你正在討論支持各種版本的應用程序,然後檢查你的清單文件的最小和目標sdk版本你也設置。也可以在你的代碼中使用asynctask。 – khubaib

+0

我已經告訴過你,你需要啓動一個新的'Thread'或者使用'AsyncTask',很多例子都可以通過搜索這個網站很容易找到,甚至可以通過android文檔 – tyczj

1

看起來像是遇到NetworkOnMainThreadException。 你可以嘗試設置嚴格模式關閉使用:

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

不過,我勸你應該將任何長期運行的操作,或網絡運營到後臺線程,就像一個AsyncTask

在這裏,你去與異步任務

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
    new LongOperation().execute(); 
} 

private class LongOperation extends AsyncTask<Void, Void, Void> { 
     String results; 
     @Override 
     protected void doInBackground(String... params) { 
       DefaultHttpClient client = new DefaultHttpClient(); 
HttpGet get = new HttpGet("http://saurabhgoyal.comoj.com/json_test.php"); 
HttpResponse r; 
try { 
    r = client.execute(get); 
    HttpEntity e = r.getEntity(); 
    InputStream is=e.getContent(); 
    InputStreamReader isr=new InputStreamReader(is); 
    BufferedReader reader=new BufferedReader(isr); 
    results=reader.readLine(); 

} catch (ClientProtocolException e1) { 
    // TODO Auto-generated catch block 
    Toast.makeText(this, "failure", Toast.LENGTH_SHORT).show(); 
    e1.printStackTrace(); 
} catch (IOException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 
      return "Executed"; 
     }  

     @Override 
     protected void onPostExecute(String result) { 
      Toast.makeText(this, results, Toast.LENGTH_SHORT).show(); 
     } 


    } 

希望可以解決您的問題。

+0

發現它的代碼本身很多錯誤。 –

0

你的答案就在這裏Json From Url

希望它幫助。

如果你喜歡保存,然後處理它...您可以使用:

try { 
     InputStream is = getAssets().open("drillData.json"); 
     int size = is.available(); 
     byte[] buffer = new byte[size]; 
     is.read(buffer); 
     is.close(); 
     String str = new String(buffer, "UTF-8"); 
     JSONArray arr = new JSONArray(str); 

     for (int i = 0; i < arr.length(); i++) { 
      list.add(arr.getJSONObject(i)); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

從資產

相關問題