来自php的变量不会出现在android上(使用json)

时间:2014-03-19 13:40:38

标签: java php android json

我正在尝试使用模糊制作一个Android应用程序(尚未进行整个模糊计算),因此计算将在php中完成。首先,我只是在php中进行简单的计算并尝试发送到android。但它没有显示任何内容......也没有任何错误。

这是我的PHP代码calc.php

的简单示例
     <?php

$calcresult = 56 * 100 * 2051 / 49;

echo json_encode($calcresult);
?> 

这是我的java代码JSONActivity.class

package com.example.ta2;
import java.io.BufferedReader;
import java.io.InputStream;import java.io.InputStreamReader; 
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class AturanKonsumsi extends Activity {
    private JSONObject jObject;

        private String xResult ="";
    private String url = "http://10.0.2.2/calc.php";
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.daftarmakanan);
            TextView txtResult = (TextView)findViewById(R.id.TextViewResult);
            xResult = getRequest(url);
            try {
                parse(txtResult);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        private void parse(TextView txtResult) throws Exception {
            jObject = new JSONObject(xResult);

            JSONArray menuitemArray = jObject.getJSONArray("calcresult");
            String sret="";
         for (int i = 0; i < menuitemArray.length(); i++) {
                System.out.println(menuitemArray.getJSONObject(i)
                        .getString("calcresult").toString());
                sret +=menuitemArray.getJSONObject(i).getString(
                        "calcresult").toString()+"\n";
        }
        txtResult.setText(sret);
    }

        public String getRequest(String Url){

           String sret="";
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(Url);
            try{
          HttpResponse response = client.execute(request);
              sret =request(response);

            }catch(Exception ex){
                Toast.makeText(this,"Gagal "+sret, Toast.LENGTH_SHORT).show();
        }
            return sret;

    }

        public static String request(HttpResponse response){
                  String result = "";
                    try{
                        InputStream in = response.getEntity().getContent();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder str = new StringBuilder();
                   String line = null;
                    while((line = reader.readLine()) != null){
                        str.append(line + "\n");
                    }
                        in.close();
                        result = str.toString();
                    }catch(Exception ex){
                        result = "Error";
                    }
                    return result;
                }
                }

当我运行Android应用程序时,它没有显示$ calcresult中的值,也没有错误。谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

你的php脚本正在生成的json没有引用

calcresult。检查输出。为了使你的java代码有效,你需要像这样创建json:

<?php
    $calcresult = 56 * 100 * 2051 / 49;
    $json = array( 'calcresult' => array( $calcresult ) );
    echo json_encode($json);
?> 

或者,您可以简化您的java。