在我的Android项目中,我想存储人员数据。 基本是在填写好个人详细信息表格后,按输入它将连接到服务器并从服务器数据中存储。Android http请求给出null异常
所以,我有:
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:
空 为什么值为空??它是不是连接到服务器?
你加发言权允许在你的Manifest.xml – FrancescoAzzola
是的,我已经添加了.. – Preeti
@Preeti:使用HttpURLConnection类的web服务请求。 – kevz