Unknown error: "null" when executing an HTTPPost with Parameters

0

Explanation:

I have a simple Android application, and also a NodeJS server. I have the following permissions on AndroidManifest.xml of my application:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>

I am trying to perform an HTTP Post and a Local URL, but this generates an unknown error: null , when running HTTP Post. So I have no way of knowing what's going on.

I've checked LogCat but no errors, only this error null of my Exception is showing up.

Here is the HTTP Post code:

HttpPost post = new HttpPost("http://192.168.25.32:8080/signup");
HttpParams params = new BasicHttpParams(); //eu estava usando Params, agora uso Pairs
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("name", "Teste2"));
pairs.add(new BasicNameValuePair("email", "[email protected]"));
pairs.add(new BasicNameValuePair("country", "Brazil"));
pairs.add(new BasicNameValuePair("user", "teste"));
pairs.add(new BasicNameValuePair("pass", "123456"));
post.setEntity(new UrlEncodedFormEntity(pairs));            
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(post); //aqui vai para a Exception

Question:

What am I doing wrong? How can I solve this problem?

I would also like to know why this error is being generated.

Note:

When accessing http://192.168.25.32:8080/signup I have a html form of registration running right, hence the to know that my NodeJS server is communicating right, and working, so I'm sure the error is in Android.

    
asked by anonymous 10.04.2014 / 13:20

2 answers

1
  • Is the url accessible in the device browser? I do not think this is because this is more common when we use localhost that is not recognized by the device, even the AVD.
  • Are you using a single thread to do the Post? For your code we can not know that, but it's a possible answer.
  • Try using AsyncTask :

    new TheTask().execute("http://192.168.25.32/signup");
    

    And the class would look something like this:

    class TheTask extends AsyncTask<String,String,String>
        {
    
    @Override
    protected String onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        // update textview here
        textView.setText("Server message is "+result);
    }
    
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }
    
    @Override
    protected String doInBackground(String... params) {
         try
            {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost method = new HttpPost(params[0]);
                HttpResponse response = httpclient.execute(method);
                HttpEntity entity = response.getEntity();
                if(entity != null){
                    return EntityUtils.toString(entity);
                }
                else{
                    return "No string.";
                }
             }
             catch(Exception e){
                 return "Network problem";
             }
    
    }
    }
    
        
    10.04.2014 / 16:18
    1

    In my view your code is correct, but the codes I made I never put the port next to ip, try to remove the port you added to the url and access only by ip and / signup as follows:

    HttpPost post = new HttpPost("http://192.168.25.32/signup");
    

    Then tell me if it worked.

    Hugs.

        
    10.04.2014 / 14:36