安卓应用程序连接php文件和条件简单


Android app connect php file and conditional simple

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

杰森:

{"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
  }
  }
 }

有人可以帮助我吗?对不起我的英语:(

创建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,了解如何分析字符串。