Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.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
Java 从Android应用到远程数据库的MySQL查询_Java_Php_Android_Mysql_Json - Fatal编程技术网

Java 从Android应用到远程数据库的MySQL查询

Java 从Android应用到远程数据库的MySQL查询,java,php,android,mysql,json,Java,Php,Android,Mysql,Json,我想在我的远程数据库上执行以下php查询 $result = mysqli_query($con->myconn, "SELECT id, stake, user, returns, teams, status FROM `bet` WHERE user = $user") or die(mysql_error()); 我唯一的问题是我不确定如何修改我的JSONParser类,以便能够同时将用户参数传递给数据库并接收结果。它目前看起来像这样,只允许我检索值或发送值 public clas

我想在我的远程数据库上执行以下php查询

$result = mysqli_query($con->myconn, "SELECT id, stake, user, returns, teams, status FROM `bet` WHERE user = $user") or die(mysql_error());
我唯一的问题是我不确定如何修改我的JSONParser类,以便能够同时将用户参数传递给数据库并接收结果。它目前看起来像这样,只允许我检索值或发送值

public class JSONParser {

static InputStream is = null;

static JSONObject jObj = null;

static String json = "";
// constructor


public JSONParser() {
}


// function get json from url
// by making HTTP POST or GET mehtod


public JSONObject makeHttpRequest(String url, String method,
                                  List<NameValuePair> params) {
    // Making HTTP request
    try {
        // check for request method
        if (method == "POST") {
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } else if (method == "GET") {
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {

            if (!line.startsWith("<", 0)) {
                if (!line.startsWith("(", 0)) {
                    sb.append(line + "\n");
                }
            }
        }

        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
   }
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
//函数从url获取json
//通过使用HTTP POST或GET方法
公共JSONObject makeHttpRequest(字符串url、字符串方法、,
列表参数){
//发出HTTP请求
试一试{
//检查请求方法
如果(方法==“POST”){
//请求方法为POST
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
//请求方法是GET
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){

如果(!line.startsWith(“那么,您有什么问题?您想发送什么?我想向查询发送一个字符串用户,以便它可以仅返回该用户的数据
 public class DisplayAllBets extends ActionBarActivity {
private String user1 = "user";
private static String url_all_games =      "***";
JSONParser jParser = new JSONParser();
private static final String TAG_SUCCESS = "success";
private static final String TAG_GAMELIST = "gamelist";
private static final String TAG_ID = "id";
private static final String TAG_STAKE = "stake";
private static final String TAG_RETURNS = "returns";
private static final String TAG_TEAMS = "teams";
private static final String TAG_STATUS = "status";
JSONArray allgames = null;
private ProgressDialog pDialog;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_all_bets);
    // Hashmap for ListView
    ArrayList<HashMap<String, String>> gamesList = new ArrayList<HashMap<String, String>>();

    // Loading products in Background Thread
    class LoadAllGames extends AsyncTask<String, String, String> {
        private String id;
        private String stake;
        private String user;
        private String returns;
        private String teams;
        private String status;

        */
       /**
         * Before starting background thread Show Progress Dialog
         *//*

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(DisplayAllBets.this);
            pDialog.setMessage("Loading Bets. 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_games, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Games: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Games
                    allgames = json.getJSONArray(TAG_GAMELIST);

                    // looping through All Products
                    for (int i = 0; i < allgames.length(); i++) {
                        JSONObject c = allgames.getJSONObject(i);

                        // Storing each json item in variable
                        id = c.getString(TAG_ID);
                        stake = c.getString(TAG_STAKE);
                        returns = c.getString(TAG_RETURNS);
                        status = c.getString(TAG_STATUS);
                        teams = c.getString(TAG_TEAMS);;



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

                        // adding each child node to HashMap key => value
                        map.put(TAG_ID, id);
                        map.put(TAG_TEAMS, teams);
                        map.put(TAG_STAKE, stake);
                        map.put(TAG_RETURNS, returns);
                        map.put(TAG_STATUS, status);


                        // adding HashList to ArrayList
                        gamesList.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 "";
        }