デバイスがインターネットに接続されていない場合、Toast でインターネット接続エラーが発生するはずです。しかし、アプリケーションがクラッシュします。No Internet Connection のエラーをキャッチしません。私のコードはインターネット接続で完全に動作します。私を助けてください
private class MyAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String s=postData();
return s;
}
protected void onPostExecute(String result){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);
}
public String postData() {
// Create a new HttpClient and Post Header
String origresponseText="";
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/ServletParams/AndroidServlet");
// Add your data cnic,mobileNo,name,address,nextkin
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1",cnic));
nameValuePairs.add(new BasicNameValuePair("param2", mobileNo));
nameValuePairs.add(new BasicNameValuePair("param3", name));
nameValuePairs.add(new BasicNameValuePair("param4", address));
nameValuePairs.add(new BasicNameValuePair("param5", nextkin));
nameValuePairs.add(new BasicNameValuePair("param6", sendImages));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
/* execute */
HttpResponse response = httpclient.execute(httppost);
HttpEntity rp = response.getEntity();
origresponseText=readContent(response);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), "sorry", Toast.LENGTH_SHORT).show();
}
String responseText = origresponseText.substring(7, origresponseText.length());
return responseText;
}
}
String readContent(HttpResponse response)
{
String text = "";
InputStream in =null;
try {
in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
text = sb.toString();
} catch (IllegalStateException e) {
Toast.makeText(getBaseContext(), "sorry", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getBaseContext(), "Sorry", Toast.LENGTH_SHORT).show();
}
finally {
try {
in.close();
} catch (Exception ex) {
Toast.makeText(getBaseContext(), "Sorry", Toast.LENGTH_SHORT).show();
}
}
return text;
}
ログは次のとおりです。
10-17 19:45:05.061: E/AndroidRuntime(22597): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-17 19:45:05.061: E/AndroidRuntime(22597): at android.os.Handler.<init>(Handler.java:121)
10-17 19:45:05.061: E/AndroidRuntime(22597): at android.widget.Toast$TN.<init>(Toast.java:322)
10-17 19:45:05.061: E/AndroidRuntime(22597): at android.widget.Toast.<init>(Toast.java:91)
10-17 19:45:05.061: E/AndroidRuntime(22597): at android.widget.Toast.makeText(Toast.java:238)
10-17 19:45:05.061: E/AndroidRuntime(22597): at com.example.androidufoneapp.CustomerRegistrationL0$MyAsyncTask.postData(CustomerRegistrationL0.java:647)
10-17 19:45:05.061: E/AndroidRuntime(22597): at com.example.androidufoneapp.CustomerRegistrationL0$MyAsyncTask.doInBackground(CustomerRegistrationL0.java:602)
10-17 19:45:05.061: E/AndroidRuntime(22597): at com.example.androidufoneapp.CustomerRegistrationL0$MyAsyncTask.doInBackground(CustomerRegistrationL0.java:1)
10-17 19:45:05.061: E/AndroidRuntime(22597): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-17 19:45:05.061: E/AndroidRuntime(22597): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-17 19:45:05.061: E/AndroidRuntime(22597): ... 4 more