2

如果用户名和密码正确,则必须显示“SUCCESS”,否则必须显示“FAILED”。我正在使用BasicNameValuePair. 以及它NullPointerException在这条线上的显示int code = pres.getStatusLine().getStatusCode();

public class MyPostActivity extends Activity {
    DefaultHttpClient client;
    HttpPost post;
    HttpResponse res;
    HttpEntity ent;
    Button b;
    List<NameValuePair> pairs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b = (Button) findViewById(R.id.button1);
        client = new DefaultHttpClient();
        post = new HttpPost(
                "http://somesite.com/abc");
        b.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                List<NameValuePair> pairs = new ArrayList<NameValuePair>(3);
                pairs.add(new BasicNameValuePair("Email", "avinash"));
                pairs.add(new BasicNameValuePair("password", "avinash2"));

                try {
                    post.setEntity(new UrlEncodedFormEntity(pairs));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                HttpResponse pres = null;
                try {
                    pres = client.execute(post);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                int code = pres.getStatusLine().getStatusCode();
                if (code == 200) {
                    Toast.makeText(getApplicationContext(), "Successful",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Failed!",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

}

在此处输入图像描述

4

3 回答 3

4

您应该将 Internet 权限添加到该AndroidManifest.xml文件。此外,您不应在UI Thread. 事实上,这Http execute将导致UI Thread挂起等待来自服务器的响应。这将导致ANR(应用程序没有响应)。我建议你阅读以下文章

于 2013-03-26T11:24:50.540 回答
1

请将此权限也添加到您的清单并尝试

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

和 AsyncTask 代码

private class SignInService extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        try {
            progressDilaog = ProgressDialog.show(MainActivity.this, "",
                    "Loading", true, false);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected Void doInBackground(Void... params) {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
        nameValuePairs.add(new BasicNameValuePair("username", "abc"));
        nameValuePairs.add(new BasicNameValuePair("password", "bcd"));

        try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            try {
              HttpResponse response = httpclient.execute(httppost);

              int responsecode = response.getStatusLine().getStatusCode();


            } catch (ClientProtocolException e) {
              e.printStackTrace();
            } catch (IOException e) {
              e.printStackTrace();
            }
         } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
         }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        progressDilaog.dismiss();


    }
}
于 2013-03-26T11:33:20.257 回答
0
        try {
            pres = client.execute(post);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int code = pres.getStatusLine().getStatusCode();

好吧,空指针异常发生是因为您可能在您的行中遇到异常client.execute并注意到它(您只需捕获它)。这就是为什么pres为空。您应该打印异常以找到您真正的问题。

于 2013-03-26T11:25:58.777 回答