林开发使用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;
}
}
任何想法,以帮助我吗? 谢谢。
您是否在清单中设置了您需要的权限?尝试在通过USB电缆连接到计算机的手机上运行应用程序,然后复制logcat错误并将其放入您的问题中,以便我们可以帮助您。 – George
不要在主UI线程上执行网络I/O。看到'NetworkOnMainThreadException'的logcat或任何其他问题。 – laalto
应用程序与堆栈跟踪粉碎。它在哪里? –