林開發使用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
應用程序與堆棧跟蹤粉碎。它在哪裏? –