从Android获取PHP脚本的POST值,并在Android中显示JSON结果

时间:2015-06-20 18:01:42

标签: php android json

我有一个PHP脚本,只有在从Android传递的值时才会给出JSON结果。例如if(isset($_POST["storeName"]))。它要求Android将storeName的值发送到PHP脚本,只有脚本才会返回JSON结果。

String abc = "abc";

JSON结果的示例如下:

{"storelist":[{"DESCRIPTION":"1. Mango Magic \r\n2. All Berry Bang\r\n3. Strawberry Juice\r\n4. Banana Buzz"}]}

我想要的就是从上面的JSON中获取DESCRIPTION。 这里的问题是,如何将abc的值从Android传递给PHP并获取JSON结果并将其存储在Android的变量中?

1 个答案:

答案 0 :(得分:0)

这是你的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 method
    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) {
                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;

    }

在doinbackground方法

中的asynctask中建立连接
@Override
    protected String doInBackground(String... strings) {
        String query = "abc";
        JSONParser jsonParser = new JSONParser();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("query", query));

        json = jsonParser.makeHttpRequest(url_get_providers,
                "POST", params);
        return json.toString();
    }