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

Previously, I could remotely access a string from php. It's hard for me at first, but AsyncTask did the job for me. Now I can access the query results from php to sql server. But I would like to pass a string from my java class to php, and since I was looking for some information I saw the JSON message and got the codes, but I cannot 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();
      

Run codeHide result


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

+3


source to share


1 answer


If you received the response as JSON format from the server, first make the json string into JSONObject. 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 has an answer to your question. After making the connection to the url, just add a parameter to send your data to the server using an OutputStreamWriter, just like you would for receiving a response using an 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

      

0


source







All Articles