Unknown Error: "null" when running an Httppost with Parameters

Asked

Viewed 410 times

0

Explanation:

I own a simple Android app, 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 mistakes, just that mistake null of my Exception is appearing.

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.

Observing:

When accessing http://192.168.25.32:8080/signup I have an html registration form working right, from there to know that my Nodejs server is communicating right, and working, so I’m sure the error is on Android.

2 answers

1

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

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

then tell me if it worked.

Hugs.

  • But this way the port to be accessed will be 80 by default and the port that my server is listening to is 8080

  • But yes, I tested another post on my apache server that is on port 80 and I continue with the same unknown error null

1


  1. Is the url accessible in the device browser? I believe this is not the case, because this is more common when using localhost that is not recognized by the device, even AVD.
  2. Are you using a single thread to do the Post? By your code we cannot know this, but it is a possible answer.

Try to use 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";
         }

}
}
  • Yes, it is accessing normal in the device’s browser. However in the code the HTTP Post does not work and does not tell me the error, only warns "null" I will try to use a Thread to do the post, because I am doing it directly on Activity onCreate

  • With this code there, it doesn’t work. the onPostExecute method doesn’t exist in Asynctask because the eclipse warns: The method onPostExecute(Void) of type TheTask must override or implement a supertype method

  • @Pauloroberto Try to change the class by extending to AsyncTask<String, Void, String> and the method for onPostExecute(String result).

  • Guy using that AsyncTask worked! I made some changes, as for example I removed the @Override of onPostExecute(Void result) and changed to String result the parameter but this was only to compile, the problem was that I was not using a separate Task(task) to perform the HTTP Post! I didn’t even know it needed to be done in a separate task to work :) now it’s okay here man, thank you!

  • But I would like to know how to make the onPostExecute method work...

  • Still still I got it, thanks anyway ! :)

  • I found another problem, kkkk, only works the first time the Task, has to open and close again the activity to try again

  • @Pauloroberto, I’m sorry I didn’t pass a complete code because I was running out of time. I’m glad you were able to solve the problem. If you want I can put more information about "asynchronicity" on android.

  • I’d like you to put information on how to run one again AsyncTask without having to re-start Activity

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.