2013-11-02 36 views
0

我想從sql server檢索數據,數據庫的網關是一個php腳本。沒關係。makeHttpRequest崩潰

我創建了一個ListView類,只要一個項目被選中,一個新的活動就是startet。當對象被選中

  public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) 
      { 
       // getting values from selected ListItem 
       String pid = ((TextView) view.findViewById(R.id.pid)).getText() 
         .toString(); 
       String catID = prodCatCrossList.get(pid); 

       // Starting new intent 
       Intent in = new Intent(getApplicationContext(), 
         EditItemActivity.class); 
       // sending parameters to next activity 
       in.putExtra(TAG_PID, pid); 
       in.putExtra(TAG_CATID, catID); 

       // starting new activity and expecting some response back 
       startActivityForResult(in, 100); 
      } 

所傳遞的參數都很好我仔細檢查了這個

活動1來電活性2(「EditItemActivity」):在這個活動開始時,一個HTTP GET請求來完成。

現在Activity2.oncreate被稱爲:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.edit_item); 

    // getting product details from intent 
    Intent i = getIntent(); 

    // getting product id (pid) from intent 
    pid = i.getStringExtra(TAG_PID); 
    catID = i.getStringExtra(TAG_CATID); 

    // Getting complete product details in background thread 
    new GetProductDetails().execute(); 

} 

最後出故障的異步任務:類GetProductDetails擴展的AsyncTask {

protected String doInBackground(String... args) 
    { 
     // updating UI from Background Thread 
     runOnUiThread(new Runnable() { 
      public void run() { 
       // Check for success tag 
       int success; 
       try { 

        // Building Parameters 
        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
        params.add(new BasicNameValuePair("pid", pid)); 

        JSONObject json = jsonParser.makeHttpRequest(
          url_item_details, "GET", params); 
        // json success tag 
        success = json.getInt(TAG_SUCCESS); 
        if (success == 1) { 
          //........... 
         } 
        } 
       } catch (JSONException e) { 
        e.printStackTrace();       
       } 
      } 
     }); 

     return null; 
    } 

】這個params值是正確的,內部的語法json流也是對的。

我很確定「makeHttpRequest」會導致錯誤,但我不明白爲什麼?在activity1中,我已經從我的服務器中成功檢索到數據...

誰可以提供幫助?創建

以下錯誤消息:

11-02 16:27:12.053: E/AndroidRuntime(17563): FATAL EXCEPTION: main 
11-02 16:27:12.053: E/AndroidRuntime(17563): android.os.NetworkOnMainThreadException 
11-02 16:27:12.053: E/AndroidRuntime(17563): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at java.net.InetAddress.getAllByName(InetAddress.java:214) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at com.dafocus.shoporganizer.JSonParser.makeHttpRequest(JSonParser.java:63) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at com.dafocus.shoporganizer.EditItemActivity$GetProductDetails$1.run(EditItemActivity.java:136) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at android.os.Handler.handleCallback(Handler.java:615) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at android.os.Looper.loop(Looper.java:137) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at android.app.ActivityThread.main(ActivityThread.java:4921) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at java.lang.reflect.Method.invokeNative(Native Method) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at java.lang.reflect.Method.invoke(Method.java:511) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
11-02 16:27:12.053: E/AndroidRuntime(17563): at dalvik.system.NativeStart.main(Native Method) 
11-02 16:27:52.873: D/dalvikvm(17563): GC_CONCURRENT freed 142K, 13% free 10241K/11719K, paused 2ms+1ms, total 16ms 

回答

2

doInbackground刪除runOnUiThread。您正在runOnUiThread(在ui線程上)執行與網絡相關的操作。

您需要在doInbackground這是在後臺線程

0

就像@Raghunandan說你明確做網絡工作在主線程調用運行與網絡相關的操作。負責處理所有Android應用程序的Dalvik虛擬機不允許這樣做。這就是爲什麼一旦它的主線程發出一個網絡(在這種情況下是JSON)請求,你的應用程序就會崩潰。