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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测