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

时间:2013-09-29 20:08:58

标签: php android json

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

JSON:

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

Condicional:

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

任何人都可以帮助我吗?对不起我的英文:(

1 个答案:

答案 0 :(得分: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以了解如何解析字符串。