android httppost错误

时间:2013-02-23 19:37:03

标签: php android mysql

基本上我在数据库端设置了一些成员,我还做了一个注册表单,它将数据发布到数据库,但我遇到的问题是我的程序不会启动活动。当我注释掉它运行的httppost部分时,所以我相信我的问题与PHP文件有关,但是已经发布了我已经完成的java代码。在另一个问题中,我将发布PHP代码。

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();
}

3 个答案:

答案 0 :(得分:0)

在没有查看堆栈跟踪的情况下很难知道问题,但我想说的问题是在UI(主)线程上发出网络请求。

您需要在单独的线程上执行所有网络任务。

请参阅此link获取教程。

答案 1 :(得分:0)

您是否请求过互联网权限?

    <uses-permission android:name="android.permission.INTERNET" />

答案 2 :(得分:0)

    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();
}