0

サーバーからのデータを表示したいのですが、できません。これが私のJavaコードです

public class AllProductsActivity extends ListActivity {

private ProgressDialog mProgressDialog;
JSONParser jparser = new JSONParser();
ArrayList<HashMap<String, String>> productlist;
private static String url = "http://localhost/android_connect/get_all_products.php";

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";

JSONArray product = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_product);

    productlist = new ArrayList<HashMap<String, String>>();
    new LoadAllProducts().execute();
    ListView lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int postion, long id) {

            String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                    .toString();
            Intent i = new Intent(getApplicationContext(),
                    EditProductActivity.class);
            i.putExtra(TAG_PID, pid);
            startActivityForResult(i, 100);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == 100) {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }
}

class LoadAllProducts extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(AllProductsActivity.this);
        mProgressDialog.setTitle("Loading");
        mProgressDialog.setMessage("Please Wait");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jparser.makeHttpRequset(url, "GET", params);
        Log.d("All Product:", json.toString());
        try {
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                product = json.getJSONArray(TAG_PRODUCTS);
                for (int i = 0; i < product.length(); i++) {
                    JSONObject c = product.getJSONObject(i);
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);
                    productlist.add(map);
                }
            } else {
                Intent i = new Intent(getApplicationContext(),
                        NewProductActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {

            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String file_url) {
        mProgressDialog.dismiss();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {

                ListAdapter adapter = new SimpleAdapter(
                        AllProductsActivity.this, productlist,
                        R.layout.list_item, new String[] { TAG_PID,
                                TAG_NAME },
                        new int[] { R.id.pid, R.id.name });
                setListAdapter(adapter);

            }
        });
enter code here
    }

}
}

これは私のlogcatです

08-15 03:38:04.313: E/AndroidRuntime(1560): FATAL EXCEPTION: AsyncTask #1
08-15 03:38:04.313: E/AndroidRuntime(1560): java.lang.RuntimeException: An error occured while executing doInBackground()
08-15 03:38:04.313: E/AndroidRuntime(1560):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at java.lang.Thread.run(Thread.java:856)
08-15 03:38:04.313: E/AndroidRuntime(1560): Caused by: java.lang.NullPointerException
08-15 03:38:04.313: E/AndroidRuntime(1560):     at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:496)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at com.sd.androidphp.JSONParser.makeHttpRequset(JSONParser.java:53)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at com.sd.androidphp.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:91)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at com.sd.androidphp.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-15 03:38:04.313: E/AndroidRuntime(1560):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-15 03:38:04.313: E/AndroidRuntime(1560):     ... 4 more
08-15 03:38:07.803: E/WindowManager(1560): Activity com.sd.androidphp.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d0cfd8 V.E..... R.....ID 0,0-320,162} that was originally added here
08-15 03:38:07.803: E/WindowManager(1560): android.view.WindowLeaked: Activity com.sd.androidphp.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d0cfd8 V.E..... R.....ID 0,0-320,162} that was originally added here
08-15 03:38:07.803: E/WindowManager(1560):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
08-15 03:38:07.803: E/WindowManager(1560):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
08-15 03:38:07.803: E/WindowManager(1560):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
08-15 03:38:07.803: E/WindowManager(1560):  at android.app.Dialog.show(Dialog.java:281)
08-15 03:38:07.803: E/WindowManager(1560):  at com.sd.androidphp.AllProductsActivity$LoadAllProducts.onPreExecute(AllProductsActivity.java:84)
08-15 03:38:07.803: E/WindowManager(1560):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
08-15 03:38:07.803: E/WindowManager(1560):  at android.os.AsyncTask.execute(AsyncTask.java:534)
08-15 03:38:07.803: E/WindowManager(1560):  at com.sd.androidphp.AllProductsActivity.onCreate(AllProductsActivity.java:46)
08-15 03:38:07.803: E/WindowManager(1560):  at android.app.Activity.performCreate(Activity.java:5104)
08-15 03:38:07.803: E/WindowManager(1560):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-15 03:38:07.803: E/WindowManager(1560):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-15 03:38:07.803: E/WindowManager(1560):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-15 03:38:07.803: E/WindowManager(1560):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-15 03:38:07.803: E/WindowManager(1560):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-15 03:38:07.803: E/WindowManager(1560):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 03:38:07.803: E/WindowManager(1560):  at android.os.Looper.loop(Looper.java:137)
08-15 03:38:07.803: E/WindowManager(1560):  at android.app.ActivityThread.main(ActivityThread.java:5039)
08-15 03:38:07.803: E/WindowManager(1560):  at java.lang.reflect.Method.invokeNative(Native Method)
08-15 03:38:07.803: E/WindowManager(1560):  at java.lang.reflect.Method.invoke(Method.java:511)
08-15 03:38:07.803: E/WindowManager(1560):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-15 03:38:07.803: E/WindowManager(1560):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-15 03:38:07.803: E/WindowManager(1560):  at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

0

Android では、localhostを使用してアクセスし10.0.2.2ます。詳細はこちら

private static String url = "http://localhost/android_connect/get_all_products.php";

する必要があります

private static String url = "http://10.0.2.2/android_connect/get_all_products.php";
于 2013-08-15T05:13:46.570 に答える