Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 调用web服务时应用程序崩溃_Android_Mysql_Json - Fatal编程技术网

Android 调用web服务时应用程序崩溃

Android 调用web服务时应用程序崩溃,android,mysql,json,Android,Mysql,Json,我需要从MySQL数据库收集数据并显示在Listview中,所以我创建了一个web服务并将其托管在服务器上。然后我开始开发应用程序,但我无法得到结果,我的应用程序崩溃了,错误日志上没有错误 这是我显示结果的代码 package com.hotelapp.test; import android.app.Activity; import android.app.ListActivity; import android.app.ProgressDialog; import android.cont

我需要从MySQL数据库收集数据并显示在Listview中,所以我创建了一个web服务并将其托管在服务器上。然后我开始开发应用程序,但我无法得到结果,我的应用程序崩溃了,错误日志上没有错误

这是我显示结果的代码

package com.hotelapp.test;

import android.app.Activity;
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.*;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AllProductsActivity extends ListActivity {

    //Progress Dialog

    private ProgressDialog pDialog;


    //JSONPraser
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String,String>> productsList;

//url to get records
private static String url_all_products = "http://54.243.58.156/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_HID="hid";
private static final String TAG_NAME="name";

JSONArray products = null;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_products);

    //Hashmap for List

    productsList = new ArrayList<HashMap<String, String>>();

    //Load Products In Backgound
    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 hid = ((TextView) view.findViewById(R.id.hid)).getText().toString();
            //Starting new Intnet
            Intent in = new Intent(getApplicationContext(),EditProductActivity.class);
            in.putExtra(TAG_HID,hid);
            startActivityForResult(in,100);
        }
    });
}

@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
     * */

    protected void onPerExecute(){
        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
     * */
    @Override
    protected String doInBackground(String... strings) {
       // Building Parameters
        List<NameValuePair> param = new ArrayList<NameValuePair>();

        JSONObject json = jParser.makeHttpRequest(url_all_products,"GET",param);

        Log.d("All Products: ",json.toString());

        try {
            //Cehck for SUCCESS TAG


            int success = json.getInt(TAG_SUCCESS);

            if(success ==1){
               //Get Product Arra

                products = json.getJSONArray(TAG_PRODUCTS);

                //Looping arrays
                for(int i=0; i<products.length();i++){
                    JSONObject c = products.getJSONObject(i);

                    String id = c.getString(TAG_HID);
                    String name = c.getString(TAG_NAME);

                    //creating new Hashmap
                    HashMap<String, String>map = new HashMap<String, String>();

                    map.put(TAG_HID,id);
                    map.put(TAG_NAME,name);

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

    protected void onPostExecute (String file_url){
        //dismiss the dialog after getting all products
        pDialog.dismiss();

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ListAdapter adapter = new SimpleAdapter(
                        AllProductsActivity.this,productsList,R.layout.list_item,new String[]{TAG_HID,TAG_NAME},
                        new int[]{R.id.hid,R.id.name}
                );

                setListAdapter(adapter);
            }
        });
    }
}
}
package com.hotelapp.test;
导入android.app.Activity;
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.*;
导入org.apache.http.NameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
公共类AllProductsActivity扩展了ListActivity{
//进度对话框
私人对话;
//杰森普拉泽
JSONParser jParser=新的JSONParser();
ArrayList productsList;
//获取记录的url
私有静态字符串url\u所有产品=”http://54.243.58.156/get_all_products.php";
//Json节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_PRODUCTS=“PRODUCTS”;
私有静态最终字符串标记_HID=“HID”;
私有静态最终字符串标记_NAME=“NAME”;
JSONArray产品=null;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.all_产品);
//列表的哈希映射
productsList=新的ArrayList();
//将产品装载到后台
新建LoadAllProducts().execute();
//获取列表视图
ListView lv=getListView();
//论单产品的选择
//启动编辑产品屏幕
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//从选定的ListItem获取值
字符串hid=((TextView)view.findViewById(R.id.hid)).getText().toString();
//开始新的Intnet
Intent in=newintent(getApplicationContext(),EditProductActivity.class);
in.putExtra(标签隐藏,隐藏);
startActivityForResult(in,100);
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
//如果结果代码为100
如果(结果代码==100){
//如果收到结果代码100
//指用户编辑/删除的产品
//重新加载此屏幕
Intent=getIntent();
完成();
星触觉(意向);
}
}
/**
*通过发出HTTP请求加载所有产品的后台异步任务
* */
类LoadAllProducts扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
受保护的void onPerExecute(){
super.onPreExecute();
pDialog=newprogressdialog(AllProductsActivity.this);
setMessage(“正在加载产品…请稍候”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/**
*从url获取所有产品
* */
@凌驾
受保护的字符串背景(字符串…字符串){
//建筑参数
List param=new ArrayList();
JSONObject json=jParser.makeHttpRequest(url_all_products,“GET”,param);
Log.d(“所有产品:,json.toString());
试一试{
//Cehck成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//获取产品阵列
products=json.getJSONArray(TAG_products);
//循环数组

对于(int i=0;i我猜您的问题是因为AsyncTask中的一个方法命名不正确:

protected void onPerExecute(){
应该是:

protected void onPreExecute(){

您可能会得到一个空指针,因为您想要在此方法中创建的资源实际上没有被创建。

我猜您的问题是因为您的AsyncTask中的一个方法命名不正确:

protected void onPerExecute(){
应该是:

protected void onPreExecute(){

您可能得到了一个空指针,因为您想在此方法中创建的资源实际上没有被创建。

要检测此类拼写错误,最好使用注释@overwrite。如果名称不正确,它将产生编译错误。它似乎是意外删除的,以检测su拼写错误是使用注释@overwrite的一种好做法。如果名称不正确,则会产生编译错误。它似乎被意外删除了

是否将INTERNET权限添加到清单中?日志中一定有错误或警告是否将INTERNET权限添加到清单中?没有错误日志中没有错误或警告