我为此苦苦挣扎..请帮助我。我也附上logcat输出..请帮助我。我从本地主机url调用数据库,因为我将数据库保留在XAMPP mysql中。问题在于ListView根本不显示任何内容。应用程序已崩溃。
public class Delivery_info extends ListActivity {
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 = "http:/127.0.0.1/hul/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "product";
private static final String TAG_DATE = "created_on";
private static final String TAG_DELIVERY = "delivery_id";
private static final String TAG_INVOICE = "invoice_amt";
// products JSONArray
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delivery_info);
// 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 AdapterView.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(), Finances.class);
// sending pid to next activity
in.putExtra(TAG_PRODUCTS, 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);
}
}
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Delivery_info.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 response
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_PRODUCTS);
String name = c.getString(TAG_DATE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PRODUCTS, id);
map.put(TAG_DATE, 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(
Delivery_info.this, productsList,
R.layout.list_item, new String[]{TAG_PRODUCTS,
TAG_DATE},
new int[]{R.id.pid, R.id.name});
// updating listview
setListAdapter(adapter);
}
});
}
}
}
03-27 18:55:53.805 19554-19750/com.example.hul.hul E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.IllegalArgumentException: Host name may not be null
at org.apache.http.HttpHost.<init>(HttpHost.java:83)
at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.example.hul.hul.JSONParser.makeHttpRequest(JSONParser.java:63)
at com.example.hul.hul.Delivery_info$LoadAllProducts.doInBackground(Delivery_info.java:129)
at com.example.hul.hul.Delivery_info$LoadAllProducts.doInBackground(Delivery_info.java:107)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
03-27 18:55:54.065 19554-19554/com.example.hul.hul E/WindowManager﹕ Activity com.example.hul.hul.Delivery_info has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4204c9d0 V.E..... R......D 0,0-684,192} that was originally added here
android.view.WindowLeaked: Activity com.example.hul.hul.Delivery_info has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4204c9d0 V.E..... R......D 0,0-684,192} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:350)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:289)
at com.example.hul.hul.Delivery_info$LoadAllProducts.onPreExecute(Delivery_info.java:119)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.example.hul.hul.Delivery_info.onCreate(Delivery_info.java:59)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.access$600(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
03-27 19:07:07.163 21510-21529/com.example.hul.hul E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.IllegalArgumentException: Host name may not be null
at org.apache.http.HttpHost.<init>(HttpHost.java:83)
at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.example.hul.hul.JSONParser.makeHttpRequest(JSONParser.java:63)
at com.example.hul.hul.Delivery_info$LoadAllProducts.doInBackground(Delivery_info.java:129)
at com.example.hul.hul.Delivery_info$LoadAllProducts.doInBackground(Delivery_info.java:107)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
03-27 19:07:07.534 21510-21510/com.example.hul.hul E/WindowManager﹕ Activity com.example.hul.hul.Delivery_info has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4204d2c0 V.E..... R......D 0,0-684,192} that was originally added here
android.view.WindowLeaked: Activity com.example.hul.hul.Delivery_info has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4204d2c0 V.E..... R......D 0,0-684,192} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:350)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:289)
at com.example.hul.hul.Delivery_info$LoadAllProducts.onPreExecute(Delivery_info.java:119)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.example.hul.hul.Delivery_info.onCreate(Delivery_info.java:59)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.access$600(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
最佳答案
您在这里有2个问题:
1- 127.0.0.1不应在移动设备上使用,因为它是指服务器
这不是手机本身。因此,将该IP更改为服务器或测试机(安装了xampp的服务器)的实际IP地址
2-URL本身有一个错误(典型):http后面缺少/:
private static String url_all_products = "http:/127.0.0.1/hul/get_all_products.php";
应该
private static String url_all_products = "http://127.0.0.1/hul/get_all_products.php";
因此您的最终网址应类似于(示例):
private static String url_all_products = "http://192.168.1.10/hul/get_all_products.php";
甚至是域名:
private static String url_all_products = "http://my.api.domain/hul/get_all_products.php";
关于android - E/AndroidRuntime:致命异常:AsyncTask#2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36248087/