发出post请求抛出nullpointerexception

时间:2014-05-13 00:51:34

标签: java android http post

我正在尝试从我的某个Android应用程序发出帖子请求,但HttpClient.execute方法出于某种原因出错。

在我的应用程序中,我有一个makePostRequest方法,用于发出帖子请求。方法如下:

// Makes a post request to the server
public void makePostRequest(String url, ArrayList<NameValuePair> postData) {
    HttpClient client = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost();

    try {
        boolean prnull = postRequest == null;
        Log.d("DEBUG:", String.valueOf(prnull));
        postRequest.setEntity(new UrlEncodedFormEntity(postData));
        HttpResponse response = client.execute(postRequest);
        Log.i("INFO:", response.toString());
    } catch (ClientProtocolException e) {
        Log.d("DEBUG:", e.toString());
    } catch (IOException e) {
        Log.d("DEBUG:", e.toString());
    }

}

在活动中我将其用作:

    // Process user registration
    Button registerButton = (Button) findViewById(R.id.registerButton);
    registerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ArrayList<NameValuePair> postData = new ArrayList<NameValuePair>(2);
            postData.add(new BasicNameValuePair("email", "testemail@gmail.com"));
            postData.add(new BasicNameValuePair("password", "abcdxyz"));
            makePostRequest(register_url, postData);
        }
    });

以下是stacktrace:

05-13 06:12:55.888: E/AndroidRuntime(21553): FATAL EXCEPTION: main
05-13 06:12:55.888: E/AndroidRuntime(21553): Process: com.example.unishots, PID: 21553
05-13 06:12:55.888: E/AndroidRuntime(21553): java.lang.NullPointerException
05-13 06:12:55.888: E/AndroidRuntime(21553):    at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:496)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at com.example.unishots.ShotsActivity.makePostRequest(ShotsActivity.java:61)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at com.example.unishots.RegisterActivity$2.onClick(RegisterActivity.java:42)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at android.view.View.performClick(View.java:4633)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at android.view.View$PerformClick.run(View.java:19330)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at android.os.Handler.handleCallback(Handler.java:733)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at android.os.Looper.loop(Looper.java:157)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at android.app.ActivityThread.main(ActivityThread.java:5356)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at java.lang.reflect.Method.invoke(Method.java:515)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-13 06:12:55.888: E/AndroidRuntime(21553):    at dalvik.system.NativeStart.main(Native Method)

我已经检查过postRequest不为null,因为这是传递给execute方法的唯一参数。这可能有什么问题?

更新

url被定义为ShotsActivity类中RegisterActivity扩展的字符串变量。网址定义如下:

// Server url variables
public String base_url = "http://192.168.2.8:8000";
public String login_url = base_url + "/login/";
public String register_url = base_url + "/api/v1/users";

1 个答案:

答案 0 :(得分:5)

我认为问题在于,尽管您将String url传递给makePostRequest(),但您不会在任何地方使用它。也许你应该把它传递给HttpPost( url