2014-05-02 79 views
-2

我的應用程序在仿真器上工作良好,但是當我在智能手機上運行它(Galaxy S3)後,它在此活動中崩潰,其中存在HTTP連接!哪裏有問題?代碼或導出?當我點擊連接按鈕時,它崩潰。應用程序在模擬器上工作,但它不能在智能手機上工作

public class login extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.login); 
    final MyApplication MyApp = (MyApplication) this.getApplication(); 

    ViewGroup layout = (ViewGroup) findViewById(R.id.login); 
    MyApp.changeFonts(layout); 

    if (!MyApp.IsConnect()) 
     {Toast.makeText(getBaseContext(), "Connessione non disponibile", Toast.LENGTH_SHORT).show();} 
    final Button fer = (Button) findViewById(R.id.invia); 
    fer.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      String url = "http://corraphp.altervista.org/server.php"; 
      EditText txtNome = (EditText) findViewById(R.id.txtNome); 
      EditText txtTessera = (EditText) findViewById(R.id.txtCodice); 

      String cognome = txtNome.getText().toString(); 
      String tessera = txtTessera.getText().toString(); 

      ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
      pairs.add(new BasicNameValuePair("cognome",cognome)); 
      pairs.add(new BasicNameValuePair("tessera", tessera)); 
      if ((cognome.equals("")) && (tessera.equals(""))) 
      {Toast.makeText(getBaseContext(), "Inserire il cognome e la tessera", Toast.LENGTH_LONG).show(); return;} 
      else 
      { 
      if (cognome.equals("")) 
      { Toast.makeText(getBaseContext(), "Inserire il cognome", Toast.LENGTH_LONG).show(); return;} 
      if (tessera.equals("")) 
      { Toast.makeText(getBaseContext(), "Inserire la tessera", Toast.LENGTH_LONG).show(); return;} 
      } 
      InputStream is = null; 
      StringBuilder sb=null; 
      String result=null; 

      //http post 
      try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(url); 
       httppost.setEntity(new UrlEncodedFormEntity(pairs)); 
       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()); 
      } 

      //paring data 
      try{ 
      JSONArray jArray = new JSONArray(result); 
      JSONObject json_data=null; 
      int id = 0; 
      String Cognome = ""; 
      for(int i=0;i<jArray.length();i++){ 
        json_data = jArray.getJSONObject(i); 
        id = json_data.getInt("id"); 
        Cognome = json_data.getString("cognome"); 
      } 

      MyApp.setUtente(json_data.getInt("id")); 
      MyApp.setCognome(json_data.getString("cognome")); 
      MyApp.setTessera(tessera); 
      Intent i = new Intent(login.this, saluto.class); 
      startActivity(i); 
      finish(); 
      }catch(JSONException e1){ 
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(login.this);  
        alertDialogBuilder.setTitle("error");  
        alertDialogBuilder.setMessage("Cognome/Tessera errati"); 

        alertDialogBuilder.setNeutralButton("ok",new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog,int id) { 
           dialog.cancel(); 

          } 
         }); 
        AlertDialog alertDialog = alertDialogBuilder.create(); 
        // show alert 
        alertDialog.show(); 

      }catch (ParseException e1){ 
       e1.printStackTrace(); 
      } 
     } 
    });}} 
+1

你可以進入logcat並打印出來t他收到錯誤信息? – jmcdonnell40

+0

你的Android設備的版本是什麼? – Salman

+0

我的設備版本是Android 4.3!我沒有logcat,因爲我在我的設備上運行我的應用程序,沒有USB – Corra

回答

0

從你的代碼看來,網絡操作似乎在應用程序的UI線程/主線程中訪問的onClickListener中。

檢查O.S.仿真器和設備上的版本:

爲什麼你的應用程序會崩潰在Android版本3.0及以上,但在Android 2.x上正常工作的原因是因爲蜂窩和冰淇淋三明治(及以上),O.S.對UI線程的濫用要嚴格得多。例如,Android設備運行蜂窩或以上時檢測到UI線程上的網絡連接,NetworkOnMainThreadException將被拋出:

E/AndroidRuntime(673): java.lang.RuntimeException: Unable to start activity 
    ComponentInfo{com.example/com.example.ExampleActivity}: android.os.NetworkOnMainThreadException 

的解釋,爲什麼發生這種情況是有據可查的Android developer's site

當應用程序 嘗試在其主線程上執行聯網操作時引發NetworkOnMainThreadException。僅針對定位到Honeycomb SDK或更高版本的應用程序拋出了 。 針對早期SDK版本的應用程序允許在其主要事件循環線程上執行聯網,但不鼓勵使用 。

去走遍:

Why the app would crash or work depending on O.S.

Try AsyncTask to avoid NetworkOnMainThread

爲什麼你不使用嚴格模式選擇,因爲你的解決方案,只是爲了調試(我建議避免這種情況也確實,你知道現在有什麼問題):
Critical to fix it, not by setting Thread policies

相關問題