2012-04-18 76 views
0

我爲android寫了一個應用程序,我有一個問題。它適用於手機,但不適用於平板電腦。 我有錯誤:http連接android.os.networkonmainthreadexception錯誤。這是我的代碼:http連接錯誤android.os.networkonmainthreadexception

public class AktualizacjaActivity { public static final String KEY_121 =「http://xxx.php」;

public String getServerData(String returnString,Context context) { 
    DatabaseAdapter db1 = new DatabaseAdapter(context); 
    db1.open(); 
    String question=""; 
    try 
    { 

     Cursor c = db1.makeCursor(DatabaseAdapter.STRUCT, new String[] {DatabaseAdapter.ID},DatabaseAdapter.KODE_ID+"='"+returnString+"'",null); 
     c.moveToFirst(); 
     question+="NOT IN('"; 
     while(!c.isLast()) 
     { 
     question+=c.getString(c.getColumnIndex(DatabaseAdapter.ID))+"','"; 
     c.moveToNext(); 
     } 
     question+=c.getString(c.getColumnIndex(DatabaseAdapter.ID))+"')"; 

    } 
    catch(Exception e) 
    { 

    } 

    db1.close(); 
    if(question.equalsIgnoreCase("NOT IN('")) question=""; 


    InputStream is = null; 

    String result = ""; 
    //the year data to send, warunek rok większy od 1980 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("code_id",returnString)); 
    nameValuePairs.add(new BasicNameValuePair("no", question)); 

    //http post 
    try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(KEY_121); 
      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-2"),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()); 
    } 
    //parse json data 
    DatabaseAdapter db = new DatabaseAdapter(context); 
    db.open(); 
    try{ 


      JSONArray jArray = new JSONArray(result); 

      for(int i=0;i<jArray.length();i++){ 
        JSONObject json_data = jArray.getJSONObject(i); 

        db.addItmem("INSERT INTO my_kodeks_struktura VALUES(" 
        +json_data.getInt("id")+"," 
        +json_data.getInt("code_id")+","+ 
        json_data.getInt("position_art")+",'"+ 
        json_data.getString("position_art_a")+"',"+ 
        json_data.getInt("level")+",'"+ 
        json_data.getString("name")+"');"); 



      } 


    }catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 
    db.close(); 
    return "Upd"; 

}  



} 
+0

這是一個API> = 13,po prostu nieróboperacjizwiązanychzsieciąnagłównymwątkuaplikacji ...użyjAsyncTask albowątków – Selvin 2012-04-18 11:46:03

回答

2

這就像例外說的那樣:你不能在ui線程上進行http調用。至少不在較新的操作系統上。

考慮使用AsyncTask進行此類調用。