gpt4 book ai didi

java - Android 应用程序在模拟器上运行正常,但在真实设备上崩溃

转载 作者:行者123 更新时间:2023-11-29 20:26:15 25 4
gpt4 key购买 nike

我正在开发一个使用 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");
}

}
});

我认为应用程序在执行这一行时会崩溃:

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

这是我的网络服务模块:

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;
}
}

最佳答案

您的网络服务的 URL 在您的机器上是本地的。模拟器工作正常,因为它可能在同一网络上。

但是这个网址:http://192.168.10.110:2233/您的设备无法访问。这就是它出现超时错误并且您的应用程序崩溃的原因。

如果你想在你的设备上测试这个,也许你需要使用公共(public)网络或使用 wifi 和一些代理工具,如 Charles

希望你现在清楚了这个问题。

关于java - Android 应用程序在模拟器上运行正常,但在真实设备上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32501168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com