嗨我仍然在空闲时间学习android,我正在尝试使用Json从android上的MySQL数据库中收集数据,但我在处理线程时遇到了问题。
任何暗示指向这个问题的正确方向?也许知道错误在哪里?
我是android的新手
JsonParser如下:
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;
}
}
ListActivity如下:
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);
}
}
堆栈跟踪也是:
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
在我活动的第36行,我有: super.onCreate(savedInstanceState);
也在Main.xml中我有:
<?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>
AndroidManifest.xml具有以下权限
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 0 :(得分:2)
如果您将ListActivity
扩展到您的活动,则您的Main.xml
必须包含ID为的Listview
android:id="@android:id/list"
,所以先改变它......
答案 1 :(得分:0)
问题是,DataconectActivity
扩展了ListActivity
类型,而您正在设置内容查看LinearLayout
(如main.xml所示)。
您可以在http://developer.android.com/resources/tutorials/views/hello-listview.html
找到有关列表视图和活动的更多信息因此,一个解决方案是DataconectActivity扩展了Activity类,因此它可以在LinearLayout视图中显示(在main.xml
中定义)。
其他解决方案,只是删除代码的第36行,并让ListActivity将ListView加载为默认值。