我正在努力解决这个问题。请帮助我。我也附加了logcat输出..请帮我解决这个问题。我从localhost 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)
答案 0 :(得分:2)
你有2个问题:
不应在移动设备上使用1- 127.0.0.1 ,因为它指的是服务器 这不是手机本身。所以将IP更改为服务器或测试机器的实际IP地址(安装xampp的地方)
2-你在网址本身有错误(拼写错误):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";
答案 1 :(得分:1)
如果您的服务响应在外部(PostMan)完成,那么您必须签入Java代码意味着清单中的Internet权限。