我的應用程序在仿真器上工作良好,但是當我在智能手機上運行它(Galaxy S3)後,它在此活動中崩潰,其中存在HTTP連接!哪裏有問題?代碼或導出?當我點擊連接按鈕時,它崩潰。應用程序在模擬器上工作,但它不能在智能手機上工作
public class login extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final MyApplication MyApp = (MyApplication) this.getApplication();
ViewGroup layout = (ViewGroup) findViewById(R.id.login);
MyApp.changeFonts(layout);
if (!MyApp.IsConnect())
{Toast.makeText(getBaseContext(), "Connessione non disponibile", Toast.LENGTH_SHORT).show();}
final Button fer = (Button) findViewById(R.id.invia);
fer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String url = "http://corraphp.altervista.org/server.php";
EditText txtNome = (EditText) findViewById(R.id.txtNome);
EditText txtTessera = (EditText) findViewById(R.id.txtCodice);
String cognome = txtNome.getText().toString();
String tessera = txtTessera.getText().toString();
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("cognome",cognome));
pairs.add(new BasicNameValuePair("tessera", tessera));
if ((cognome.equals("")) && (tessera.equals("")))
{Toast.makeText(getBaseContext(), "Inserire il cognome e la tessera", Toast.LENGTH_LONG).show(); return;}
else
{
if (cognome.equals(""))
{ Toast.makeText(getBaseContext(), "Inserire il cognome", Toast.LENGTH_LONG).show(); return;}
if (tessera.equals(""))
{ Toast.makeText(getBaseContext(), "Inserire la tessera", Toast.LENGTH_LONG).show(); return;}
}
InputStream is = null;
StringBuilder sb=null;
String result=null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//paring data
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
int id = 0;
String Cognome = "";
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
id = json_data.getInt("id");
Cognome = json_data.getString("cognome");
}
MyApp.setUtente(json_data.getInt("id"));
MyApp.setCognome(json_data.getString("cognome"));
MyApp.setTessera(tessera);
Intent i = new Intent(login.this, saluto.class);
startActivity(i);
finish();
}catch(JSONException e1){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(login.this);
alertDialogBuilder.setTitle("error");
alertDialogBuilder.setMessage("Cognome/Tessera errati");
alertDialogBuilder.setNeutralButton("ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show alert
alertDialog.show();
}catch (ParseException e1){
e1.printStackTrace();
}
}
});}}
你可以進入logcat並打印出來t他收到錯誤信息? – jmcdonnell40
你的Android設備的版本是什麼? – Salman
我的設備版本是Android 4.3!我沒有logcat,因爲我在我的設備上運行我的應用程序,沒有USB – Corra