Java 发出post请求会引发nullpointerexception
我试图从我的一个android应用程序发出post请求,但由于某种原因,Java 发出post请求会引发nullpointerexception,java,android,http,post,Java,Android,Http,Post,我试图从我的一个android应用程序发出post请求,但由于某种原因,HttpClient.execute方法出错 在我的应用程序中,我有一个makePostRequest方法,用于发出post请求。方法如下: // Makes a post request to the server public void makePostRequest(String url, ArrayList<NameValuePair> postData) { HttpClient client
HttpClient.execute
方法出错
在我的应用程序中,我有一个makePostRequest方法,用于发出post请求。方法如下:
// 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());
}
}
// 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";
我已经检查了postRequest
是否为null,因为这是传递给execute
方法的唯一参数。这里可能出了什么问题
更新
URL被定义为ShotsActivity
类中的字符串变量,该类RegisterActivity
扩展了该类。URL的定义如下:
// 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());
}
}
// 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";
我认为问题在于,尽管您将字符串url传递给makePostRequest(),但您不会在任何地方使用它。也许您应该将其传递到HttpPost(url)您在哪里定义和使用url?我认为您应该将url传递到HttpPost@SaraSeppolaURL在类中定义为
String
变量,请参见更新。谢谢。您应该将url作为新的HttpPost(url)传递。没问题,从其他人那里更容易找到bug代码:-)