Search code examples
androidandroidhttpclient

android http request giving null exception


In My Android project I want to store person data. The basic is, after fill up person details form,when press enter it will connect to server and from server data will store.

So, I have:

 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();
    }

and mServerAuth code is:

    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;
        }}

To access this code I am getting error:

Signup access server error: null why the value is null??Is it not connecting to the server??


Solution

  • Well u shdn't use deprecated apis in Android. Below is the code using HTTPUrlConnection api. Hope it help to serve ur purpose.

    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();