2015-09-10 44 views
1

林開發使用MySQL數據庫和JSON的Android應用程序,它可以在仿真器正常,但是當IM啓動它我的設備上(銀河S5)IM密切面臨力 這裏是我的請求,服務器並獲取JSON對象:Android應用程序的工作原理currectly上仿真器,但墜毀在真實設備

btnFaalSazi.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      String validatePhoneNumber = phoneNumber.getText().toString(); 
      if (validatePhoneNumber.matches("^(?:0\\d{10}|9\\d{9})$")) { 

       txtError.setText(""); 
       structUsers.register_number = phoneNumber.getText().toString(); 
       String phone = structUsers.register_number; 

       Log.i("LOG", phone); 
       Log.i("LOG", "HELOOOOOO"); 

       final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("register_number", phone)); 

       String result = Webservice.readUrl("http://192.168.10.110:2233/api/register", params); 

       if (result != null) { 
        try { 
         G.users.clear(); 
         JSONObject object = new JSONObject(result); 
         String status = object.optString("status"); 
         String code = object.optString("code"); 
         String message = object.optString("message"); 

         Log.i("LOG", "status hast " + status); 
         Log.i("LOG", "code hast " + code); 
         Log.i("LOG", "mesage hast " + message); 

         if (status != null && code != null) { 
          if (Integer.parseInt(status) == -1) { 
           Intent intent = new Intent(ActivityRegisterNumber.this, ActivityRegisterCode.class); 
           intent.putExtra("REGISTERNUMBER", structUsers.register_number); 
           ActivityRegisterNumber.this.startActivity(intent); 
          } 
         } 

         if (status != null && message != null) { 
          if (Integer.parseInt(status) == 100) { 
           Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); 
           Log.i("LOG", "after error 100"); 
          } else if (Integer.parseInt(status) == 101) { 
           Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); 
           Log.i("LOG", "after error 101"); 
          } else if (Integer.parseInt(status) == 102) { 
           Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); 
           Log.i("LOG", "after error 102"); 
          } else if (Integer.parseInt(status) == 103) { 
           Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); 
           Log.i("LOG", "after error 103"); 
          } 
         } 
        } 
        catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      } else { 
       txtError.setText("Wrong phone number"); 
      } 

     } 
    }); 

我認爲應用程序崩潰時,它要執行這一行:

    String result = Webservice.readUrl("http://192.168.10.110:2233/api/register", params); 

,這是我的web服務模塊:

public class Webservice { 

public static String readUrl(String url, ArrayList<NameValuePair> params) { 

    try { 

     HttpClient client = new DefaultHttpClient(); 
     HttpPost method = new HttpPost(url); 

     if (params != null) { 
      method.setEntity(new UrlEncodedFormEntity(params)); 
     } 

     HttpResponse response = client.execute(method); 

     InputStream inputStream = response.getEntity().getContent(); 
     String result = convertInputStreamToString(inputStream); 

     return result; 
    } 
    catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 


public static String convertInputStreamToString(InputStream inputStream) { 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 
     StringBuilder builder = new StringBuilder(); 

     String line = ""; 

     while ((line = reader.readLine()) != null) { 
      builder.append(line); 
     } 

     return builder.toString(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 
} 

任何想法,以幫助我嗎? 謝謝。

+0

您是否在清單中設置了您需要的權限?嘗試在通過USB電纜連接到計算機的手機上運行應用程序,然後複製logcat錯誤並將其放入您的問題中,以便我們可以幫助您。 – George

+0

不要在主UI線程上執行網絡I/O。看到'NetworkOnMainThreadException'的logcat或任何其他問題。 – laalto

+0

應用程序與堆棧跟蹤粉碎。它在哪裏? –

回答

0

我找到答案,所以我解釋了誰將會看到這篇文章後,

的Android 3.0之後,我們應該使用的AsyncTask用於發送和接收web服務。

我的模擬器是Android 2.2的和我的設備是Android 4.4系統,因此應用程序完美工作在模擬器,但崩潰的裝置。

0

你給你的應用程序的特權?某些安全軟件會阻止您的應用程序訪問網絡。

2

您的web服務的URL是本地到您的計算機。 模擬器正常工作,因爲它可能位於同一網絡上。

但這個網址:http://192.168.10.110:2233/無法通過該設備訪問。 這就是爲什麼它會出現一些超時錯誤並導致應用程序崩潰。

如果你要測試的這款設備上,也許你需要使用一個公共網絡或使用wifi和一些代理的工具,像Charles

希望現在的問題是清楚的給你。

+0

感謝您的答案,但我的設備和我的虛擬服務器是在同一個網絡和相同的IP範圍,你有另一個想法? –

相關問題