简体   繁体   中英

How to pass and read string from android to sql using JSON

Previously, i can access the string from php remotely. I find it difficult at first but AsyncTask did the work for me. Now, i can access the result of the query from php to sql server. But I would like to pass a string from my java class to php and as I googled some information, i saw some JSON post and get codes but i can't clearly understand them. Here's my code:

 protected String doInBackground(Void... params) { BufferedReader br = null; StringBuilder sb = new StringBuilder(); String url = "http://122.2.8.226/MITBookstore/sqlconnect.php"; HttpURLConnection urlConnection = null; String line; try { urlConnection = (HttpURLConnection) new URL(url).openConnection(); InputStream in = urlConnection.getInputStream(); br = new BufferedReader(new InputStreamReader(in)); while ((line = br.readLine()) != null) { sb.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } } } return sb.toString(); 

The string is contained in "sb.toString()". Now how would I add a JSON something in my code to send string from java to php, and also get the result string from php to java as well. Thanks in advance for any help.

If you receive response as JSON format from server, make the json string to JSONObject first. And then read the json data for your use.

try {
    JSONObject obj = new JSONObject(sb.toString());   // make string to json obj

    Iterator iter = obj.keys();     // get all keys from json obj and iterating
    while(iter.hasNext()){
        String key = (String)iter.next();
        String str = obj.get(key).toString();

       // write your code
    }
} catch(Exception e) {
    e.printStackTrace();
}

Your code already contains the answer of your question. After make url connection, just add parameter for sending your data to server with OutputStreamWriter as like you did for receive the response with InpustStreamReader.

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        String url = "http://122.2.8.226/MITBookstore/sqlconnect.php";
        HttpURLConnection urlConnection = null;

        String line;
        try {
            urlConnection = (HttpURLConnection) new URL(url).openConnection();

            // wrtie params
            OutputStreamWriter we = new OutputStreamWriter(urlConnection.getOutPutStream());
            wr.write(data);       // data (make json obj to 'key=value' string)
            wr.flush();
            wr.close();
            // read response
            InputStream in = urlConnection.getInputStream();

            br = new BufferedReader(new InputStreamReader(in));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        }
        catch (Exception e) {
           e.printStackTrace();
        }
        finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }enter code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM