2015-09-10 45 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范围,你有另一个想法? –

相关问题