0
Meine App stürzt ab und ich kann den Fehler nicht finden.Wie erhalte ich von meiner App die Nachricht "Net not connected" anstelle des Absturzes
Ich möchte es mir eine Nachricht sagen, dass das Netzwerk nicht verbunden ist.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class SimpleForm extends Activity {
private EditText editTextUserName;
private EditText editTextPassword;
//public static final String USER_NAME = "USERNAME";
String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registeration);
editTextUserName = (EditText) findViewById(R.id.et_username_ID);
editTextPassword = (EditText) findViewById(R.id.et_pass_ID);
}
public void invokeLogin(View view){
username = editTextUserName.getText().toString();
password = editTextPassword.getText().toString();
login(username,password);
}
private void login(final String username, String password) {
class LoginAsync extends AsyncTask<String, Void, String>{
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(SimpleForm .this, "Please wait", "Loading...");
}
@Override
protected String doInBackground(String... params) {
//String uname = params[0];
// String pass = params[1];
JSONObject jsonObject = new JSONObject();
try {
jsonObject.accumulate("username", params[0]);
} catch (JSONException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
try {
jsonObject.accumulate("password", params[1]);
} catch (JSONException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
jsonObject.accumulate("deviceToken", "2324h5gj345gj3h4g5j");
} catch (JSONException e5) {
// TODO Auto-generated catch block
e5.printStackTrace();
}
try {
jsonObject.accumulate("os", "android");
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
jsonObject.accumulate("key", "MEu07MgiuWgXwJOo7Oe1aHL0yM8P");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 4. convert JSONObject to JSON to String
String dataString = jsonObject.toString();
//String dataString = {"username":"apitest","password":"123456","deviceToken":"2324h5gj345gj3h4g5j34g","os":"andriod","key":"MEu07MgiuWgXwJOo7Oe1aHL0yM8VvP"}
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", dataString));
//nameValuePairs.add(new BasicNameValuePair("password", pass));
String result = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://mobile.betfan.com/api/?action=login");
//http://mobile.betfan.com/api/?action=login
//http://192.168.1.10/mobiletest/process.php
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
protected void onPostExecute(String result){
String s = result.trim();
loadingDialog.dismiss();
JSONObject respObject;
try {
respObject = new JSONObject(s);
String active = respObject.getString("status_message");
if(active.equalsIgnoreCase("success")){
Toast.makeText(getApplicationContext(), s+"Login successfull", Toast.LENGTH_LONG).show();
Intent intent=new Intent(SimpleForm.this,Welcome.class);
startActivity(intent);
// Intent intent = new Intent(MainActivity.this, UserProfile.class);
//intent.putExtra(USER_NAME, username);
// finish();
//startActivity(intent);
}else {
Toast.makeText(getApplicationContext(), "Login Fail", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
LoginAsync la = new LoginAsync();
la.execute(username, password);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sie für eine Methode suchen sind, ob ein Gerät ein Netzwerk überprüfen wird Verbindung? – joshgoldeneagle
Diese Frage ist vergleichbar mit dieser http://stackoverflow.com/questions/27796001/prevent-app-from-crash-when-network-unavailable, überprüfen Sie die erste Antwort, dort können Sie eine Methode zum Erkennen von Netzwerkverbindungen vor dem Start finden Ihre AsyncTask. – mbenegas
nur sollte es mir eine Nachricht geben, wenn das Netz nicht verbunden ist, und App sollte nicht gehen "leider my-App wurde gestoppt". Ich will das nur. @joshgoldeneagle –