我找到了一个可以帮助我的TUTORIEL ..这个使用MySQL数据库获取数据并在ListView控件中显示它。问题是我在处理AsyncTask时遇到错误..所以这是我的个人代码和错误报告
package pre.commande;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> produitList;
// url to get all products list
private static String url_all_products = "http://10.0.2.2/precommande/ts_produit.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUIT = "produit";
private static final String TAG_ID = "id";
private static final String TAG_DES = "des";
// products JSONArray
JSONArray produit = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Hashmap for ListView
produitList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
produit = json.getJSONArray(TAG_PRODUIT);
// looping through All Products
for (int i = 0; i < produit.length(); i++) {
JSONObject c = produit.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String des = c.getString(TAG_DES);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_DES, des);
// adding HashList to ArrayList
produitList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, produitList,
R.layout.list_item, new String[] { TAG_ID,
TAG_DES},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
错误报告
08-06 11:07:04.979:D / dalvikvm(1347):GC_EXTERNAL_ALLOC释放43K,51%免费2669K / 5379K,外部1541K / 1559K,暂停132ms 08-06 11:07:05.369:W / System.err(1347):java.net.SocketException:权限被拒绝 08-06 11:07:05.369:W / System.err(1347):at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method) 08-06 11:07:05.369:W / System.err(1347):at dalvik.system.BlockGuard $ WrappedNetworkSystem.socket(BlockGuard.java:335) 08-06 11:07:05.369:W / System.err(1347):at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216) 08-06 11:07:05.369:W / System.err(1347):at java.net.Socket.checkOpenAndCreate(Socket.java:802) 08-06 11:07:05.369:W / System.err(1347):at java.net.Socket.connect(Socket.java:948) 08-06 11:07:05.379:W / System.err(1347):at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 08-06 11:07:05.379:W / System.err(1347):at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143) 08-06 11:07:05.379:W / System.err(1347):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 08-06 11:07:05.459:W / System.err(1347):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 08-06 11:07:05.459:W / System.err(1347):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359) 08-06 11:07:05.459:W / System.err(1347):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 08-06 11:07:05.459:W / System.err(1347):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 08-06 11:07:05.459:W / System.err(1347):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 08-06 11:07:05.459:W / System.err(1347):在pre.commande.JSONParser.makeHttpRequest(JSONParser.java:62) 08-06 11:07:05.459:W / System.err(1347):在pre.commande.MainActivity $ LoadAllProducts.doInBackground(MainActivity.java:105) 08-06 11:07:05.459:W / System.err(1347):在pre.commande.MainActivity $ LoadAllProducts.doInBackground(MainActivity.java:1) 08-06 11:07:05.459:W / System.err(1347):在android.os.AsyncTask $ 2.call(AsyncTask.java:185) 08-06 11:07:05.459:W / System.err(1347):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:306) 08-06 11:07:05.469:W / System.err(1347):at java.util.concurrent.FutureTask.run(FutureTask.java:138) 08-06 11:07:05.469:W / System.err(1347):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 08-06 11:07:05.469:W / System.err(1347):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581) 08-06 11:07:05.469:W / System.err(1347):at java.lang.Thread.run(Thread.java:1019) 08-06 11:07:05.469:E / Buffer Error(1347):转换结果java.lang.NullPointerException时出错 08-06 11:07:05.469:E / JSON Parser(1347):解析数据时出错org.json.JSONException:字符0处的输入结束 08-06 11:07:05.491:W / dalvikvm(1347):threadid = 8:线程退出,未捕获异常(group = 0xb67714f0) 08-06 11:07:05.491:E / AndroidRuntime(1347):致命异常:AsyncTask#1 08-06 11:07:05.491:E / AndroidRuntime(1347):java.lang.RuntimeException:执行doInBackground()时发生错误 08-06 11:07:05.491:E / AndroidRuntime(1347):在android.os.AsyncTask $ 3.done(AsyncTask.java:200) 08-06 11:07:05.491:E / AndroidRuntime(1347):at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:274) 08-06 11:07:05.491:E / AndroidRuntime(1347):at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 08-06 11:07:05.491:E / AndroidRuntime(1347):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:308) 08-06 11:07:05.491:E / AndroidRuntime(1347):at java.util.concurrent.FutureTask.run(FutureTask.java:138) 08-06 11:07:05.491:E / AndroidRuntime(1347):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 08-06 11:07:05.491:E / AndroidRuntime(1347):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581) 08-06 11:07:05.491:E / AndroidRuntime(1347):at java.lang.Thread.run(Thread.java:1019) 08-06 11:07:05.491:E / AndroidRuntime(1347):引起:java.lang.NullPointerException 08-06 11:07:05.491:E / AndroidRuntime(1347):在pre.commande.MainActivity $ LoadAllProducts.doInBackground(MainActivity.java:108) 08-06 11:07:05.491:E / AndroidRuntime(1347):在pre.commande.MainActivity $ LoadAllProducts.doInBackground(MainActivity.java:1) 08-06 11:07:05.491:E / AndroidRuntime(1347):在android.os.AsyncTask $ 2.call(AsyncTask.java:185) 08-06 11:07:05.491:E / AndroidRuntime(1347):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:306) 08-06 11:07:05.491:E / AndroidRuntime(1347):......还有4个 08-06 11:07:05.528:D / gralloc_goldfish(1347):未检测到GPU仿真的仿真器。 08-06 11:07:06.318:E / WindowManager(1347):活动pre.commande.MainActivity泄露了最初添加的窗口com.android.internal.policy.impl.PhoneWindow$DecorView@b6c6afc0 08-06 11:07:06.318:E / WindowManager(1347):android.view.WindowLeaked:Activity pre.commande.MainActivity泄露了最初添加的窗口com.android.internal.policy.impl.PhoneWindow$DecorView@b6c6afc0这里 08-06 11:07:06.318:E / WindowManager(1347):在android.view.ViewRoot。(ViewRoot.java:258) 08-06 11:07:06.318:E / WindowManager(1347):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 08-06 11:07:06.318:E / WindowManager(1347):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 08-06 11:07:06.318:E / WindowManager(1347):在android.view.Window $ LocalWindowManager.addView(Window.java:424) 08-06 11:07:06.318:E / WindowManager(1347):在android.app.Dialog.show(Dialog.java:241) 08-06 11:07:06.318:E / WindowManager(1347):在pre.commande.MainActivity $ LoadAllProducts.onPreExecute(MainActivity.java:95) 08-06 11:07:06.318:E / WindowManager(1347):在android.os.AsyncTask.execute(AsyncTask.java:391) 08-06 11:07:06.318:E / WindowManager(1347):在pre.commande.MainActivity.onCreate(MainActivity.java:64) 08-06 11:07:06.318:E / WindowManager(1347):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-06 11:07:06.318:E / WindowManager(1347):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 08-06 11:07:06.318:E / WindowManager(1347):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 08-06 11:07:06.318:E / WindowManager(1347):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 08-06 11:07:06.318:E / WindowManager(1347):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 08-06 11:07:06.318:E / WindowManager(1347):在android.os.Handler.dispatchMessage(Handler.java:99) 08-06 11:07:06.318:E / WindowManager(1347):在android.os.Looper.loop(Looper.java:130) 08-06 11:07:06.318:E / WindowManager(1347):在android.app.ActivityThread.main(ActivityThread.java:3683) 08-06 11:07:06.318:E / WindowManager(1347):at java.lang.reflect.Method.invokeNative(Native Method) 08-06 11:07:06.318:E / WindowManager(1347):at java.lang.reflect.Method.invoke(Method.java:507) 08-06 11:07:06.318:E / WindowManager(1347):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 08-06 11:07:06.318:E / WindowManager(1347):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-06 11:07:06.318:E / WindowManager(1347):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
在清单文件中添加以下行。
<uses-permission android:name="android.permission.INTERNET" />
以上<application>
代码。