Reputation: 32949
I am trying to make a post request from one of my android applications, but the HttpClient.execute
method errors out for some reason.
In my application I have a makePostRequest method that I use to make post requests. The method is as follows:
// 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());
}
}
and in the activity I use this as:
// 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", "[email protected]"));
postData.add(new BasicNameValuePair("password", "abcdxyz"));
makePostRequest(register_url, postData);
}
});
Following is the 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)
I have already checked that postRequest
isn't null as this is the only argument passed to the execute
method. What possibly could be wrong here ?
update
The urls are defined as String variables in the ShotsActivity
class which RegisterActivity
extends. The urls are defined as follows:
// 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";
Upvotes: 2
Views: 1594
Reputation: 679
I think the problem is that although you pass your String url to the makePostRequest(), you dont use it anywhere. Perhaps you should pass it on to the HttpPost(url)
Upvotes: 6