Pedro Teran
Pedro Teran

Reputation: 1200

(Json with MySQL) E/AndroidRuntime(338):

Hi I'm still learning android on my free time, I'm trying to collect data from a MySQL DB on android using Json, but I have a problem handling the threads.

any hint to point on the right direction on this problem? maybe to know where the error is?
I'm relatively new to android

the JsonParser comes as following

package com.remotedata.firstapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JsonParser {
    static InputStream is = null;
    static JSONObject json_data = null;
    static String result = "";

    // constructor
    public JsonParser() {
    }

    public  JSONObject getJSONFromUrl(ArrayList<NameValuePair> nameValuePairs, String url) {

        //http post this will keep the same way as it was (it's important to do not forget to add Internet access to androidmanifest.xml
        InputStream is = null;
        String result ="";
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        }
        catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }

        //convert response that we receive from the php file into a String()
        try{


            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        // try parse the string to a Json object
        try {
            json_data = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return Json String
        return json_data;           


    }    
}

the ListActivity is the following:

package com.remotedata.firstapp;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;




public class DataconectActivity extends ListActivity {
    // adding strings that are going to be the tags of the nodes for the list view in the ListAdapter
    private static String TAG_PRODID = "codigo";
    private static final String TAG_NOMBRE = "nombre";
    private static final String TAG_DESCRIPCION = "descripcion";
    private static final String TAG_COLOR = "color";
    private static final String TAG_PESO =  "peso";
    private static final String TAG_PRECIO = "precio";
    private static final String TAG_DISPONIBILIDAD = "disponibilidad";
    private static final String TAG_INFO = "resultados";
    // remember to change the url to try it with the web server http://trialsols.webege.com/mobil.php after this is important to add at least 10.000 items to the db to ensure the query efficiency
    private static final String url ="http://192.168.1.50/mobilv1.php";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Hashmap for ListView
        ArrayList<HashMap<String, String>> jsonResultList = new ArrayList<HashMap<String, String>>();

        //the price data to send  must change in the application so it will be bundle with a text box 
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("precio","10"));

        // Creating JSON Parser instance
        JsonParser jParser = new JsonParser();

        // getting JSON string from URL
        JSONObject json_data = jParser.getJSONFromUrl(nameValuePairs, url);
        JSONArray jArray = null;


        try {
            // Getting Array of JsonData
            jArray = json_data.getJSONArray(TAG_INFO);
            // looping through All JsonData 
            for(int i = 0; i < jArray.length(); i++){
                JSONObject c = jArray.getJSONObject(i);
                String nombre = c.getString("nombre");
                String prodid = c.getString("prodid");
                String descripcion = c.getString("descripcion");
                String color = c.getString("color");
                String peso = c.getString("peso");
                String precio = c.getString("precio");
                String disponibilidad = c.getString("disponibilidad");  
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_PRODID, prodid);
                map.put(TAG_NOMBRE, nombre);
                map.put(TAG_DESCRIPCION, descripcion);
                map.put(TAG_COLOR, color);
                map.put(TAG_PESO, peso);
                map.put(TAG_PRECIO, precio);
                map.put(TAG_DISPONIBILIDAD, disponibilidad);
                // adding HashList to ArrayList
                jsonResultList.add(map);

            }
        }

        catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
        }                

        //adding the results of the parsed JSON data to a listView using a ListAdapter
        ListAdapter adapter = new SimpleAdapter(this, jsonResultList,
                R.layout.datalistitems,
                new String[] { TAG_NOMBRE, TAG_PRECIO, TAG_DISPONIBILIDAD }, new int[] {
                R.id.dbnombretxt, R.id.dbpreciotxt, R.id.dbdisponibilidadtxt });

        // add in a ListActivity We need to make a parser with thissetListAdapter(adapter);
        setListAdapter(adapter);    

    }
}

also the stacktrace is:

02-22 15:12:56.122: I/jdwp(338): received file descriptor 10 from ADB
02-22 15:12:56.162: D/ddm-heap(338): Got feature list request
02-22 15:12:56.353: D/AndroidRuntime(338): Shutting down VM
02-22 15:12:56.353: W/dalvikvm(338): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
02-22 15:12:56.353: E/AndroidRuntime(338): Uncaught handler: thread main exiting due to uncaught exception
02-22 15:12:56.373: E/AndroidRuntime(338): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.remotedata.firstapp/com.remotedata.firstapp.DataconectActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.app.ActivityThread.access$2100(ActivityThread.java:116)
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.os.Looper.loop(Looper.java:123)
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.app.ActivityThread.main(ActivityThread.java:4203)
02-22 15:12:56.373: E/AndroidRuntime(338):  at java.lang.reflect.Method.invokeNative(Native Method)
02-22 15:12:56.373: E/AndroidRuntime(338):  at java.lang.reflect.Method.invoke(Method.java:521)
02-22 15:12:56.373: E/AndroidRuntime(338):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-22 15:12:56.373: E/AndroidRuntime(338):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
02-22 15:12:56.373: E/AndroidRuntime(338):  at dalvik.system.NativeStart.main(Native Method)
02-22 15:12:56.373: E/AndroidRuntime(338): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.app.ListActivity.onContentChanged(ListActivity.java:236)
02-22 15:12:56.373: E/AndroidRuntime(338):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.app.Activity.setContentView(Activity.java:1620)
02-22 15:12:56.373: E/AndroidRuntime(338):  at com.remotedata.firstapp.DataconectActivity.onCreate(DataconectActivity.java:36)
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
02-22 15:12:56.373: E/AndroidRuntime(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
02-22 15:12:56.373: E/AndroidRuntime(338):  ... 11 more
02-22 15:12:56.384: I/dalvikvm(338): threadid=7: reacting to signal 3
02-22 15:12:56.384: E/dalvikvm(338): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

At line 36 of my activity I have: super.onCreate(savedInstanceState);

also in Main.xml I have this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Datos de la tabla Productos" />

    <ListView 
        android:smoothScrollbar="true"
        android:id="@+id/datalst"
        android:focusableInTouchMode="true"
        android:layout_width="fill_parent"
        android:layout_height="50dp"/>


</LinearLayout>

The AndroidManifest.xml have the following permissions

<uses-sdk android:minSdkVersion="4" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Upvotes: 0

Views: 360

Answers (2)

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

If you extends ListActivity to your Activity ,Your Main.xml must have Listview whose id is like android:id="@android:id/list" ,So change it First....

Upvotes: 2

Tomas Narros
Tomas Narros

Reputation: 13488

The problem is that your DataconectActivity extends the ListActivity type, while you are seting as its contentView a LinearLayout (as seen at main.xml).

You can find more information on List views and Activities at http://developer.android.com/resources/tutorials/views/hello-listview.html

So, a solution is that you DataconectActivity extends the Activity class, so it can show in an LinearLayout view (defined in main.xml).

Other solution, is just get rid of the line 36 of your code, and let the ListActivity load the ListView as default.

Upvotes: 0

Related Questions