0

我需要我的 android 应用程序连接到 PHP 文件(只需在表格上进行选择并以 JSON 格式显示结果) 如何将我的应用程序连接到 php 文件并设置条件?例如:

JSON:

{"productos":[{"codeqr":"hola","actions":[]}

有条件的:

if (ResultPHP==hola){
 //se encontró correctamente 'hola'
}else{
 //no se encontró 'hola'
}

目前,我的连接代码是:

 public class QRpost {

 public static void postData() {
 // Create a new HttpClient and Post Header
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("qr.php");
 try {
   // Execute HTTP Post Request
   HttpResponse response = httpclient.execute(httppost);
   HttpEntity entity = response.getEntity();
   InputStream webs = entity.getContent();
   try{
    BufferedReader reader = new BufferedReader(new InputStreamReader(webs,"iso-8859-1"),8);
    webs.close();
   }catch(Exception e){
    Log.e("log_tag", "Error al convertir el resultado");
   }
  } catch (ClientProtocolException e) {
   // TODO Auto-generated catch block
   } catch (IOException e) {
   // TODO Auto-generated catch block
  }
  }
 }

有谁能够帮我?对不起我的英语不好 :(

4

1 回答 1

0

创建您的后BufferedReader,您需要将每一行读入一个StringBuilder

        BufferedReader reader = new BufferedReader(new InputStreamReader(webs,"iso-8859-1"),8);

        StringBuilder builder = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {

            builder.append(line);
        }

        webs.close();

        String json = builder.toString();

然后,该json变量将包含所有 JSON 作为String. 然后,您将需要对该字符串使用 JSON 解析器以从 JSON 中获取所需的变量。查看JSONObjectJSONArray了解如何解析字符串。

于 2013-09-29T20:30:38.253 回答