【问题标题】:NullPointerException on Simple HttpPost简单 HttpPost 上的 NullPointerException
【发布时间】:2013-03-16 04:01:18
【问题描述】:

如果用户名和密码正确,则必须显示“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();
                }
            }
        });

    }

}

【问题讨论】:

  • 把网络服务文件也放
  • 请分享 logcat... 检查您是否在致电 execute 时收到任何异常
  • 您的网络服务代码
  • 你有上网权限吗?
  • 向我们展示您的清单文件..您需要在清单中添加互联网权限才能从您的应用程序访问互联网..您也不应该在 UI 线程上进行网络调用使用AsyncTask

标签: android http-post httprequest getjson httpexception


【解决方案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();


    }
}

【讨论】:

  • 到目前为止没有错误......而且它没有显示吐司......int responsecode = response.getStatusLine().getStatusCode(); if (responsecode == 200) { Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Failed.", Toast.LENGTH_SHORT).show(); }
  • 您不能在 doInBackground 中使用使用 Toast。如果要调试,请使用 Log.d()。
【解决方案2】:

您应该将互联网权限添加到AndroidManifest.xml 文件。此外,您不应在 UI Thread 上执行阻塞操作。事实上Http execute 将导致UI Thread 挂起等待来自服务器的响应。这将导致ANR(应用程序无响应)。我建议你阅读以下article

【讨论】:

    【解决方案3】:
            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 为空的原因。您应该打印异常以找到您真正的问题。

    【讨论】:

    • 如何打印异常??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多