我使用httpGet或httpPost将一些参数发送到我的服务器,当我的客户端连接到我的服务器时,它可以正常工作。但是当它不是时,我的应用程序崩溃了。但是,我有一个尝试/捕捉...
看看我的代码:
((ImageButton)findViewById(R.id.pickSurnom)).setOnClickListener(new OnClickListener(){
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(Postit.this);
alert.setTitle("Modifier votre surnom");
//alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(Postit.this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String surnom = input.getText().toString();
//Recuperation "id" du user dans les préférences partagées
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
String id = prefs.getString("id", null);
//------------------------ENVOI----------------------------
StringBuffer stringBuffer = new StringBuffer("");
BufferedReader bufferedReader = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet();
//httpGet.setURI(new URI("http://192.168.0.1:8888/user/public"+"?"+"surnom="+((TextView) findViewById(R.id.surnom)).getText().toString()));
httpGet.setURI(new URI("http://192.168.0.1:8888/user"+"?"+"id="+id+"surnom="+ surnom ));
HttpResponse httpResponse = httpClient.execute(httpGet);
bufferedReader = new BufferedReader(
new InputStreamReader(httpResponse.getEntity().getContent()));
String ligneLue = bufferedReader.readLine();
Toast.makeText(getApplicationContext(), "id="+bufferedReader.readLine() , Toast.LENGTH_LONG).show();
while (ligneLue!=null){
stringBuffer.append(ligneLue);
ligneLue = bufferedReader.readLine();
//Si la réponse = True
//Remplir le bon surnom dans le TextView
((TextView) findViewById(R.id.surnom)).setText(surnom);
}
} catch (Exception e){
Toast.makeText(getApplicationContext(), "Connection error", Toast.LENGTH_LONG).show();
Log.e("Exception1", e.getMessage());
} finally {
if (bufferedReader !=null){
try{
bufferedReader.close();
Toast.makeText(getApplicationContext(), "Serveur indisponible" + surnom, Toast.LENGTH_LONG).show();
} catch (IOException e){
Log.e("Exception2", e.getMessage());
}
}
}
Log.i("Exception3", stringBuffer.toString());
我的LogCat
01-17 15:45:00.801: ERROR/ActivityManager(58): ANR in com.branchu1 (com.branchu1/.Postit)
01-17 15:45:00.801: ERROR/ActivityManager(58): Reason: keyDispatchingTimedOut
01-17 15:45:00.801: ERROR/ActivityManager(58): Load: 0.5 / 0.18 / 0.08
01-17 15:45:00.801: ERROR/ActivityManager(58): CPU usage from 32503ms to 56ms ago:
01-17 15:45:00.801: ERROR/ActivityManager(58): system_server: 19% = 15% user + 3% kernel / faults: 2075 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): ndroid.launcher: 3% = 2% user + 0% kernel / faults: 1554 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): adbd: 1% = 0% user + 0% kernel
01-17 15:45:00.801: ERROR/ActivityManager(58): ndroid.settings: 0% = 0% user + 0% kernel / faults: 124 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): m.android.phone: 0% = 0% user + 0% kernel / faults: 56 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): ronsoft.openwnn: 0% = 0% user + 0% kernel / faults: 43 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): logcat: 0% = 0% user + 0% kernel / faults: 1 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): events/0: 0% = 0% user + 0% kernel
01-17 15:45:00.801: ERROR/ActivityManager(58): android.protips: 0% = 0% user + 0% kernel / faults: 23 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): zygote: 0% = 0% user + 0% kernel / faults: 135 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): .quicksearchbox: 0% = 0% user + 0% kernel / faults: 27 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): id.defcontainer: 0% = 0% user + 0% kernel / faults: 24 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): com.android.mms: 0% = 0% user + 0% kernel / faults: 25 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): m.android.music: 0% = 0% user + 0% kernel / faults: 23 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): d.process.media: 0% = 0% user + 0% kernel / faults: 25 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): m.android.email: 0% = 0% user + 0% kernel / faults: 34 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): com.svox.pico: 0% = 0% user + 0% kernel / faults: 23 minor
01-17 15:45:00.801: ERROR/ActivityManager(58): +com.branchu1: 0% = 0% user + 0% kernel
01-17 15:45:00.801: ERROR/ActivityManager(58): -com.branchu1: 0% = 0% user + 0% kernel
01-17 15:45:00.801: ERROR/ActivityManager(58): TOTAL: 33% = 25% user + 8% kernel + 0% softirq
答案 0 :(得分:1)
您的代码抛出了Android Not Responding(ANR)异常,因为您在UI线程上发出了HTTP请求。您必须确保UI线程始终响应用户。连接服务器时没有发生这种情况,因为响应足够快,但是在超时或其他问题上,你每次都会得到这个。
答案是使用AsyncTask之类的东西在不同的线程上发出HTTP请求。
答案 1 :(得分:1)
您收到“应用程序无响应”(ANR)错误。这是因为你在主线程上做了网络IO,这总是一个禁忌。如果没有网络连接,它将一直阻塞,直到它超时。
始终使用AsyncTask进行下载。
答案 2 :(得分:0)
快速猜测:在您的finally块中的try / catch中,您只能捕获IOException
。也许在.close()
上调用bufferedReader
会引发另一个异常?