duanmingting9544 2014-03-19 13:40
浏览 24
已采纳

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

I'm trying to make an android app using fuzzy (have yet to make the whole fuzzy calculation) so the calculations will be done in php. For starter I was just making a simple calculation in php first and trying to send to the android. but it doesn't show anything..there no error either.

Here's the simple example of my php code calc.php

     <?php

$calcresult = 56 * 100 * 2051 / 49;

echo json_encode($calcresult);
?> 

and this is my java code 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()+"
";
        }
        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 + "
");
                    }
                        in.close();
                        result = str.toString();
                    }catch(Exception ex){
                        result = "Error";
                    }
                    return result;
                }
                }

when I run the android app it doesn't show the value in $calcresult and there's no error either. thanks for your help

  • 写回答

1条回答 默认 最新

  • doubangzhang6992 2014-03-19 13:55
    关注

    calcresult is not referenced by the json your php script is producing. Check the output. To make your java code work, you'll need to create the json like so:

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

    Alternately, you can simplify your java.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀