android http 请求给出空异常
android http request giving null exception
在我的 Android 项目中,我想存储人员数据。
基本原理是,在填写人员详细信息表格后,当按 输入 时,它将连接到服务器并存储来自服务器的数据。
所以,我有:
private void serverSignup(final Person suf) {
new AsyncTask<Person, Void, String>() {
@Override
protected String doInBackground(Person... params) {
String serverResponse = "";
try {
serverResponse = mServerAuth.userSignup(suf.getName(),
suf.getCountry(),suf.getDate(), suf.getEmail());
} catch (Exception e) {
Log.e("ErrorMessage", TAG + " > Signup access server error: " +
e.getMessage()); **I am getting this section as null
}
return serverResponse;
}
@Override
protected void onPostExecute(String serverResponse) {
finish();
}
}.execute();
}
和mServerAuth代码是:
public String userSignup(String name, String country,
String date, String email) throws Exception {
String url = "http://localhost:8080/SpringMVCHibernate/person/add";
Person suf = new Person(name, country, date, email);
List<NameValuePair> pairs = new ArrayList<NameValuePair>(1);
pairs.add(new BasicNameValuePair("suf", new Gson().toJson(suf)));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity requestEntity = new UrlEncodedFormEntity(pairs);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(requestEntity);
try {
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
} catch (IOException ioe) {
Log.e("Error", TAG + " >
IOEException during the POST response: " + ioe.getMessage());
}
return null;
}}
要访问此代码,我收到错误消息:
Signup access server error:
空
为什么值为null??是没有连接到服务器吗??
来自HttpEntity entity = response.getEntity();
您获取了实体对象但没有读取数据,很简单,您正在 returning 空值。
你需要得到这样的回复
HttpResponse httpresponse = httpclient.execute(httppost);
String response=EntityUtils.toString(httpresponse.getEntity());
然后你需要 return "response" 字符串。
同时检查url是否正常工作?
好吧,你不会在 Android 中使用已弃用的 api。下面是使用 HTTPUrlConnection api 的代码。希望它有助于达到您的目的。
new AsyncTask < Person, Void, String > () {
String LOGIN_URL = "http://localhost:8080/SpringMVCHibernate/person/add";
HttpURLConnection conn;
DataOutputStream wr;
String responseCode = "-1";
URL url;
@Override
protected String doInBackground(String...params) {
try {
url = new URL(LOGIN_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.connect();
Person suf = new Person(name, country, date, email);
String strSuf = new Gson().toJson(suf);
wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(strSuf);
wr.close();
responseCode = conn.getResponseCode();
return responseCode;
} catch (IOException e) {
e.printStackTrace();
} finally() {
conn.disconnect();
}
return responseCode;
}
@Override
protected void onPostExecute(String responseCode) {
if (!responseCode.equals("-1"))
Toast.makeText(mContext, "Stored data successfully", Toast.LENGTH_LONG).show();
else
Toast.makeText(mContext, "failed to store data", Toast.LENGTH_LONG).show();
}
}.execute();
在我的 Android 项目中,我想存储人员数据。 基本原理是,在填写人员详细信息表格后,当按 输入 时,它将连接到服务器并存储来自服务器的数据。
所以,我有:
private void serverSignup(final Person suf) {
new AsyncTask<Person, Void, String>() {
@Override
protected String doInBackground(Person... params) {
String serverResponse = "";
try {
serverResponse = mServerAuth.userSignup(suf.getName(),
suf.getCountry(),suf.getDate(), suf.getEmail());
} catch (Exception e) {
Log.e("ErrorMessage", TAG + " > Signup access server error: " +
e.getMessage()); **I am getting this section as null
}
return serverResponse;
}
@Override
protected void onPostExecute(String serverResponse) {
finish();
}
}.execute();
}
和mServerAuth代码是:
public String userSignup(String name, String country,
String date, String email) throws Exception {
String url = "http://localhost:8080/SpringMVCHibernate/person/add";
Person suf = new Person(name, country, date, email);
List<NameValuePair> pairs = new ArrayList<NameValuePair>(1);
pairs.add(new BasicNameValuePair("suf", new Gson().toJson(suf)));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity requestEntity = new UrlEncodedFormEntity(pairs);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(requestEntity);
try {
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
} catch (IOException ioe) {
Log.e("Error", TAG + " >
IOEException during the POST response: " + ioe.getMessage());
}
return null;
}}
要访问此代码,我收到错误消息:
Signup access server error:
空
为什么值为null??是没有连接到服务器吗??
来自HttpEntity entity = response.getEntity();
您获取了实体对象但没有读取数据,很简单,您正在 returning 空值。
你需要得到这样的回复
HttpResponse httpresponse = httpclient.execute(httppost);
String response=EntityUtils.toString(httpresponse.getEntity());
然后你需要 return "response" 字符串。
同时检查url是否正常工作?
好吧,你不会在 Android 中使用已弃用的 api。下面是使用 HTTPUrlConnection api 的代码。希望它有助于达到您的目的。
new AsyncTask < Person, Void, String > () {
String LOGIN_URL = "http://localhost:8080/SpringMVCHibernate/person/add";
HttpURLConnection conn;
DataOutputStream wr;
String responseCode = "-1";
URL url;
@Override
protected String doInBackground(String...params) {
try {
url = new URL(LOGIN_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.connect();
Person suf = new Person(name, country, date, email);
String strSuf = new Gson().toJson(suf);
wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(strSuf);
wr.close();
responseCode = conn.getResponseCode();
return responseCode;
} catch (IOException e) {
e.printStackTrace();
} finally() {
conn.disconnect();
}
return responseCode;
}
@Override
protected void onPostExecute(String responseCode) {
if (!responseCode.equals("-1"))
Toast.makeText(mContext, "Stored data successfully", Toast.LENGTH_LONG).show();
else
Toast.makeText(mContext, "failed to store data", Toast.LENGTH_LONG).show();
}
}.execute();