Java 连接到MySQL?

Java 连接到MySQL?,java,android,mysql,json,fragment,Java,Android,Mysql,Json,Fragment,我使用此代码使用WAMP连接到MySQL。 但它不起作用 package com.first.ismael.material_drawer; import android.app.Activity; import android.app.Fragment; import android.app.ListFragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.vie

我使用此代码使用WAMP连接到MySQL。
但它不起作用

package com.first.ismael.material_drawer;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import  org.json.*;
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;


 import android.widget.Toast;

/**
 * Created by ismae_000 on 11/20/2015.
 */
public class all_meals extends Fragment {

   public ProgressDialog pDialog ;

// Creating JSON Parser object
    JSONParser jParser = new JSONParser();


// url to get all products list
   private static String url_all_products =               "http://192.168.1.7/get_all_products.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "component";
private static final String TAG_PID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_TYPE ="type";
ListView lv;
// products JSONArray
JSONArray products = null;

ArrayList<HashMap<String, String>> productsList;
// url to get all products list


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.all,
            container, false);

    productsList = new ArrayList<HashMap<String, String>>();
    lv =  (ListView)view.findViewById(R.id.listView2);
    // Loading products in Background Thread
   new LoadAllProducts().execute();

    // Get listview

    return view;

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}


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

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading products. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    @Override
     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
               Toast.makeText(getActivity(),"There is nothing",Toast.LENGTH_LONG).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {


      pDialog.dismiss();
        // updating UI from Background Thread
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        getActivity(), productsList,
                        R.layout.list_item, new String[]{TAG_PID,
                        TAG_NAME},
                        new int[]{R.id.pid, R.id.name});
                // updating listview
                lv.setAdapter(adapter);
            }
        });
    }
}

    }
package com.first.ismael.material\u抽屉;
导入android.app.Activity;
导入android.app.Fragment;
导入android.app.ListFragment;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.*;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.TextView;
导入android.widget.Toast;
/**
*由ismae_000于2015年11月20日创建。
*/
公共课全日制{
公共对话;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
//获取所有产品列表的url
私有静态字符串url\u所有产品=”http://192.168.1.7/get_all_products.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_PRODUCTS=“component”;
私有静态最终字符串标记_PID=“id”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记\u TYPE=“TYPE”;
ListView lv;
//产品JSONArray
JSONArray产品=null;
ArrayList productsList;
//获取所有产品列表的url
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(右布局所有,
货柜(虚假);;
productsList=新的ArrayList();
lv=(ListView)view.findViewById(R.id.listView2);
//在后台线程中加载产品
新建LoadAllProducts().execute();
//获取列表视图
返回视图;
}
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
}
类LoadAllProducts扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(getActivity());
pDialog.setMessage(“正在加载产品。请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/**
*从url获取所有产品
* */
@凌驾
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jParser.makeHttpRequest(url_all_products,“GET”,params);
//检查日志cat中的JSON响应
Log.d(“所有产品:,json.toString());
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//获取一系列产品
products=json.getJSONArray(TAG_products);
//在所有产品中循环
对于(int i=0;ivalue
地图放置(标签PID,id);
地图放置(标签名称、名称);
//将哈希列表添加到ArrayList
productsList.add(地图);
}
}否则{
//没有发现任何产品
//启动添加新产品活动
Toast.makeText(getActivity(),“什么都没有”,Toast.LENGTH_LONG.show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
@凌驾
受保护的void onPostExecute(字符串文件\u url){
pDialog.disclose();
//从后台线程更新UI
getActivity().runOnUiThread(新的Runnable()){
公开募捐{
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
getActivity(),productsList,
R.layout.list_项,新字符串[]{TAG_PID,
TAG_NAME},
新的int[]{R.id.pid,R.id.name});
//更新列表视图
低压设置适配器(适配器);
}
});
}
}
}
我在片段中使用了它。

如果您使用WAMP意味着使用localhost for database MySql,请提供帮助。本地主机的Ip地址应为10.0.2.2,而不是192.168.1.7

试着替换 _http://192.168.1.7/get_all_products.php 具有 _http://10.0.2.2/get_all_products.php

并检查您的输出get_all_products.php,这应该是json格式

读这个 -

检查json格式并使用此 - _http://jsonlint.com

什么是“不起作用”?你会犯什么错误?