android http请求给出null异常

时间:2015-12-29 08:28:39

标签: android androidhttpclient

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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

来自HttpEntity entity = response.getEntity(); 你获得实体对象但你没有读取数据,简单的就是返回null。

您需要得到像这样的回复

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

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

同时检查网址是否正常工作?