2

I had created a simplest app, a default one and I want to try to post and retrive something from a php page MyActivty.Java looks like:

package com.dolganiuc.victor.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class MainActivity extends ActionBarActivity {

    private static final String TAG = "victorMessage";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate");
        postData();


    }

    public void postData() {

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://93.117.158.187/");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            Log.i(TAG, response.toString());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

    }

}

And when Android Studio debugs that All Works fine, and i ran these app on 2 devecies Android 4.4.2 KitKat and Android 5.0 Lollipop and on both devices the application is closing retriving error Unfortunely MyApplication has closed.

What is the problem?

System.err

06-11 13:18:02.042    1142-1161/? W/System.err﹕ java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
06-11 13:18:02.042    1142-1161/? W/System.err﹕ at com.android.internal.telephony.PhoneBase.privatizeCellInfoList(PhoneBase.java:1252)
06-11 13:18:02.042    1142-1161/? W/System.err﹕ at com.android.internal.telephony.PhoneBase.getAllCellInfo(PhoneBase.java:1240)
06-11 13:18:02.042    1142-1161/? W/System.err﹕ at com.android.internal.telephony.PhoneProxy.getAllCellInfo(PhoneProxy.java:337)
06-11 13:18:02.042    1142-1161/? W/System.err﹕ at com.android.phone.PhoneInterfaceManager.getAllCellInfo(PhoneInterfaceManager.java:1486)
06-11 13:18:02.042    1142-1161/? W/System.err﹕ at com.android.internal.telephony.ITelephony$Stub.onTransact(ITelephony.java:691)
06-11 13:18:02.042    1142-1161/? W/System.err﹕ at android.os.Binder.execTransact(Binder.java:446)
6
  • 2
    Will you share the logcat error? Commented Jun 11, 2015 at 10:15
  • please connect your device with your android studio and run the app directly from studio to device on to emulator , and then show us log cat Commented Jun 11, 2015 at 10:19
  • Have you added Internet permission in the manifest file\ Commented Jun 11, 2015 at 10:19
  • yes i added internet permission and i connectected device directly from the emulator, but on logcat are hundreds lines, with errors are System.err? Commented Jun 11, 2015 at 10:26
  • turn your emulator off just debug the device on which you are running your app and then just share the logcat Commented Jun 11, 2015 at 10:28

1 Answer 1

2

The first problem may be that you didn't add the INTERNET permission in your AndroidManifest.xml. To resolve this just add this line <uses-permission android:name="android.permission.INTERNET" /> in your manifest.

The other problem is that you're trying to execute a post request on main thread. It causes NetworkOnMainThreadException. You should use AsyncTask or Handler to execute network requests, like this:

public class MainActivity extends ActionBarActivity {

    private static final String TAG = "victorMessage";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate");
        new HttpPostTask().execute();
    }

    private class HttpPostTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://93.117.158.187/");

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

                Log.i(TAG, response.toString());
                return response.toString();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }

            return null;
        }

        @Override
        protected void onPostExecute(String response) {
            super.onPostExecute(response);

            //do something with you response, connected with UI thread.
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Yes, its works thanks a lot But now on my logCat i see: I/victorMessage﹕ org.apache.http.message.BasicHttpResponse@2c904d18 how to make to show here the content of response i used Log.i(TAG, response.toString());
It depends on what your server is returning for you. Check this answer stackoverflow.com/a/10684201/3225458.
i tried this but on debuging: errorȘ connot find symol method getEntinity()
Are you properly using response.getEntity(); ?
do you have skype, to help me there?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.