问题描述
我对android完全陌生..我正在尝试一个在线发现的教程。基本目的是从数据库中检索数据并显示它。 我无法执行它。我已经附上了代码和错误日志。如果有人可以纠正错误,那将是一个很大的帮助。
<package com.example.androidhive;
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.os.Bundle;
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 AllProductsActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "192.168.0.1/android_connect/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
// products JSONArray
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_products);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditProductActivity.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* 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(AllProductsActivity.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
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} 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(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}`
错误
07-18 02:55:17.809: D/dalvikvm(2205): GC_FOR_ALLOC freed 56K, 5% free 2815K/2944K, paused 106ms, total 107ms
07-18 02:55:17.839: I/dalvikvm-heap(2205): Grow heap (frag case) to 5.628MB for 2949608-byte allocation
07-18 02:55:17.889: D/dalvikvm(2205): GC_FOR_ALLOC freed <1K, 3% free 5695K/5828K, paused 48ms, total 48ms
07-18 02:55:18.349: D/gralloc_goldfish(2205): Emulator without GPU emulation detected.
07-18 02:55:21.079: D/dalvikvm(2205): GC_FOR_ALLOC freed 19K, 2% free 6529K/6620K, paused 86ms, total 87ms
07-18 02:55:22.309: W/dalvikvm(2205): threadid=12: thread exiting with uncaught exception (group=0xb3a4fb90)
07-18 02:55:22.349: E/AndroidRuntime(2205): FATAL EXCEPTION: AsyncTask #1
07-18 02:55:22.349: E/AndroidRuntime(2205): Process: com.example.androidhive, PID: 2205
07-18 02:55:22.349: E/AndroidRuntime(2205): java.lang.RuntimeException: An error occured while executing doInBackground()
07-18 02:55:22.349: E/AndroidRuntime(2205): at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.lang.Thread.run(Thread.java:841)
07-18 02:55:22.349: E/AndroidRuntime(2205): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=192.168.0.1/android_connect/get_all_products.php
07-18 02:55:22.349: E/AndroidRuntime(2205): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
07-18 02:55:22.349: E/AndroidRuntime(2205): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
07-18 02:55:22.349: E/AndroidRuntime(2205): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-18 02:55:22.349: E/AndroidRuntime(2205): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-18 02:55:22.349: E/AndroidRuntime(2205): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-18 02:55:22.349: E/AndroidRuntime(2205): at com.example.androidhive.JSONParser.makeHttpRequest(JSONParser.java:62)
07-18 02:55:22.349: E/AndroidRuntime(2205): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:127)
07-18 02:55:22.349: E/AndroidRuntime(2205): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
07-18 02:55:22.349: E/AndroidRuntime(2205): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-18 02:55:22.349: E/AndroidRuntime(2205): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-18 02:55:22.349: E/AndroidRuntime(2205): ... 3 more
07-18 02:55:22.819: I/Choreographer(2205): Skipped 362 frames! The application may be doing too much work on its main thread.
07-18 02:55:25.829: I/Choreographer(2205): Skipped 171 frames! The application may be doing too much work on its main thread.
07-18 02:55:30.059: E/WindowManager(2205): android.view.WindowLeaked: Activity com.example.androidhive.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b40ada18 V.E..... R.....ID 0,0-461,175} that was originally added here
07-18 02:55:30.059: E/WindowManager(2205): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
07-18 02:55:30.059: E/WindowManager(2205): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
07-18 02:55:30.059: E/WindowManager(2205): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.Dialog.show(Dialog.java:286)
07-18 02:55:30.059: E/WindowManager(2205): at com.example.androidhive.AllProductsActivity$LoadAllProducts.onPreExecute(AllProductsActivity.java:117)
07-18 02:55:30.059: E/WindowManager(2205): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
07-18 02:55:30.059: E/WindowManager(2205): at android.os.AsyncTask.execute(AsyncTask.java:535)
07-18 02:55:30.059: E/WindowManager(2205): at com.example.androidhive.AllProductsActivity.onCreate(AllProductsActivity.java:57)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.Activity.performCreate(Activity.java:5243)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.ActivityThread.access$700(ActivityThread.java:135)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
07-18 02:55:30.059: E/WindowManager(2205): at android.os.Handler.dispatchMessage(Handler.java:102)
07-18 02:55:30.059: E/WindowManager(2205): at android.os.Looper.loop(Looper.java:137)
07-18 02:55:30.059: E/WindowManager(2205): at android.app.ActivityThread.main(ActivityThread.java:4998)
07-18 02:55:30.059: E/WindowManager(2205): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 02:55:30.059: E/WindowManager(2205): at java.lang.reflect.Method.invoke(Method.java:515)
07-18 02:55:30.059: E/WindowManager(2205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-18 02:55:30.059: E/WindowManager(2205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-18 02:55:30.059: E/WindowManager(2205): at dalvik.system.NativeStart.main(Native Method)
07-18 02:55:30.059: I/Choreographer(2205): Skipped 217 frames! The application may be doing too much work on its main thread.
07-18 02:55:32.999: I/Process(2205): Sending signal. PID: 2205 SIG: 9
1楼
此异常消息:
java.lang.IllegalStateException: Target host must not be n\
ull, or set in parameters. scheme=null, host=null, path=192.168.0.1/android_connect/get_all_products.php
告诉我makeHttpRequest
无法识别您传递的url中的host参数。
因此,我真的会接受@Titus的建议,并提供带有http://
前缀的完整URL(或任何协议)。
如果错误的类型发生变化,那么您可能会遇到多个错误。