[英]Android Application works correctly on emulator but crashes on real device
I'm developing an android application which use MySQL database and json, it works on emulator correctly but when im launching it on my device (Galaxy S5) I'm facing a force close Here is my request to server and get json objects: 我正在开发一个使用MySQL数据库和json的android应用程序,它可以在模拟器上正常工作,但是当我在我的设备(Galaxy S5)上将其启动时,我面临强制关闭的请求。这是我对服务器和获取json对象的请求:
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");
}
}
});
I think application crashes when its going to perform this line : 我认为应用程序在执行以下操作时会崩溃:
String result = Webservice.readUrl("http://192.168.10.110:2233/api/register", params);
and this is my webservice module : 这是我的网络服务模块:
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;
}
}
The url for your webservice is local to your machine. Web服务的URL在您的计算机本地。 The emulator works fine because possibly it is on the same network.
该仿真器可以正常工作,因为它可能在同一网络上。
But this url: http://192.168.10.110:2233/ cannot be accessed by your device. 但是您的设备无法访问该网址: http : //192.168.10.110 : 2233/ 。 That's why it is getting some timeout error and your application crashes.
这就是为什么它会出现一些超时错误,并且您的应用程序崩溃的原因。
If you want to test this on your device, maybe you need to use a common network or use wifi and some proxy tool like Charles 如果要在设备上进行测试,则可能需要使用公用网络或wifi和某些代理工具(如Charles)
Hope the issue is clear to you now. 希望这个问题现在对您很清楚。
Have you given privileges to your application? 您是否已授予您的应用程序特权? Some security software will prevent you application from accessing network.
某些安全软件将阻止您的应用程序访问网络。
I find answer so i explain it for who will read this post later, 我找到了答案,所以我为以后会阅读此帖子的人解释了这一点,
After android 3, we should use Asynctask for send and receive from webservice. 在android 3之后,我们应该使用Asynctask从webservice发送和接收。
My emulator was android 2.2 and my device was android 4.4, so application worked perfect on emulator but crashes in device. 我的模拟器是android 2.2,设备是android 4.4,因此应用程序可以在模拟器上完美运行,但在设备上崩溃。
in line 排队
String result = Webservice.readUrl("http://192.168.10.110:2233/api/register", params);
you must get ipconfig for your device (phone) not use same ip config for your emu 您必须为您的设备(电话)获取ipconfig,不要为您的emu使用相同的ip配置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.