-2

Excuse me if I don't know that I am asking a stupid question or not as a beginner.
I am retrieving values from mysql database and trying to display it in a TextView.

My database field name is "entry" and value is "444". Please tell me how I can display this value in my textview.

Home.java:

public class Home extends Activity {
    TextView tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home);
        tv = (TextView) findViewById(R.id.textView1);
        getData();
        }

    public void getData(){
        String result="";
        InputStream isr=null;
        try{
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://xxxxxx/get_data.php");
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            isr = entity.getContent();
        } catch(Exception e){
            Log.e("log_tag","Error in http connection"+e.toString());
        }
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                                            isr, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            isr.close();
            result= sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }
        try{
            JSONArray jArray=new JSONArray(result);
            JSONObject json=jArray.getJSONObject(0);
            String s= json.getString("entry");
            tv.setText(s);
        } catch(Exception e){
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
    }
}

Here is my logcat:

D/dalvikvm(276): GC_FOR_MALLOC freed 3041 objects / 204440 bytes in 283ms
W/SingleClientConnManager(276): Invalid use of SingleClientConnManager: connection still allocated.
W/SingleClientConnManager(276): Make sure to release the connection before allocating another one.
D/dalvikvm(276): GC_FOR_MALLOC freed 707 objects / 47856 bytes in 55ms
I/System.out(276): Response : User Found
E/log_tag(276): Error parsing data java.lang.NullPointerException
indivisible
  • 4,892
  • 4
  • 31
  • 50
Fayisa
  • 15
  • 5
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – M D May 30 '14 at 07:03
  • Move your http request and result parsing from MainThread to AsyncTask – mbelsky May 30 '14 at 07:04
  • Shouldn't you be using `AsyncTask` for fetching data? – MjZac May 30 '14 at 07:07
  • thank you all for your valuable time.am trying your suggestions. – Fayisa May 30 '14 at 07:11
  • The problem could be with result, json, jArray object. You should show all these objects in logcat to verify either the server response is okay and verify objects' structures. – Ahmad Raza May 30 '14 at 07:13
  • my responses are ok.i can retrieve and display sting value.but not number – Fayisa May 30 '14 at 07:21

1 Answers1

1

You are create http connection on UI tread. It can't be done. Than your result string equals "". When you create JSONArray from "" you get null.

JSONArray jArray=new JSONArray(result); // result = ""
JSONObject json=jArray.getJSONObject(0); // jArray = null <--- Excepion

Try remove try-catch block to see number of line where you get exception.

Move httpConnection in AsyncTask or in other thread.

Erzer
  • 86
  • 3
  • can you please tell me how use AsyncTask here.it showing me the error "Error parsing data android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views." – Fayisa May 30 '14 at 07:59
  • You must update interface only in UI Thread. AsyncTask got methods onPostExecute and onProgressUpdate for it. Here a good article [link](http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html) – Erzer May 30 '14 at 08:56