简体   繁体   中英

android httppost error

Basically I have set up some members on the database side, I have also done a registration form where it posts the data to the database, but the problem I'm having is that my program will not start the activity. When I comment out the httppost parts it works, so I believe that my problem is all to do with the PHP file, but have posted the java code I have done. In another question I will post the PHP code.

try
{
    httpclient = new default HttpClient();
    httppost = new HttpPost("");
    nameValuePair = new arrayList < NameValuePair > (1);
    nameValuePair . add(new BasicNameValuePair("email", email . getText() . toString() . trim()));
    nameValuePair . add(new BasicNameValuePair("password", password . getText() . toString() . trim()));
    httppost . setEntity(new UrlEncodedF or mEntity(nameValuePair));
    response = httpclient . execute(httppost);
    ResponseHandler < String > responseHandler = new BasicResponseHandler();
     final Stringresponse = httpclient . execute(httppost, responseHandler);
    loginErrorMsg . setText("" + response);

    if(response . equalsIgnorecase ("Log in Successful"))
    {
        startActivity(new Intent(LoginActivity . this, HomescreenActivity . class ));
    }
}
catch(Exceptione)
{
    e.printStackTrace();
}

It's hard to know the problem without viewing the stack trace, but I would say the issue is with making network requests on the UI(Main) thread.

You need to perform all network tasks on a separate thread.

Please see this link for a tutorial.

您是否已请求Internet权限?

    <uses-permission android:name="android.permission.INTERNET" />
    nameValuePair = new ArrayList<NameValuePair>(1);

    nameValuePair.add(new BasicNameValuePair("email", email.getText().toString().trim()));
    nameValuePair.add(new BasicNameValuePair("password", password.getText().toString().trim()));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));

    response = httpclient.execute(httppost);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    final HttpResponse responsed = httpclient.execute(httppost, responseHandler);

    final String response= EntityUtils.toString(responsed .getEntity());

    loginErrorMsg.setText(""+response);

     if(response.equalsIgnoreCase("Log in Successful"))
          startActivity(new Intent(LoginActivity.this, HomescreenActivity.class));                                                                       
}
catch(Exception e){
        e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM