我需要我的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
}
}
}
任何人都可以帮助我吗?对不起我的英文:(
答案 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中获取所需的变量。请查看JSONObject
和JSONArray
以了解如何解析字符串。