2015-12-29 869 views
0

在我的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: 为什么值为空??它是不是连接到服务器?

+0

你加发言权允许在你的Manifest.xml – FrancescoAzzola

+0

是的,我已经添加了.. – Preeti

+0

@Preeti:使用HttpURLConnection类的web服务请求。 – kevz

回答

1

那么你不会在Android中使用已弃用的apis。以下是使用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(); 
+0

@Preeti:Check使用HTTPUrlConnection的答案。 – kevz

-1

寻找文档,我看到没有DefaultHttpClient.execute(HttpPost)方法。您应该使用HttpUrlConnection作为网络请求,它是新的。 DefaultHttpClient已弃用。

0

HttpEntity entity = response.getEntity(); 你得到的实体对象,但你不读数据,简单地说,你返回null。

你需要得到这样的

HttpResponse httpresponse = httpclient.execute(httppost); 
String response=EntityUtils.toString(httpresponse.getEntity()); 

响应然后,你需要返回“响应”字符串。

同时检查网址也是它的工作与否?

+0

我也这样做了,但仍然是同样的错误..在我的连接,我使用互联网从wifi,我通过“usb teathering”连接我的PC ..在本地主机我把计算机的IP地址.. – Preeti