Android - 通过Http获取json数据并列出它们

时间:2016-03-30 23:36:39

标签: php android mysql json

我需要从服务器上生成的json中获取数据。

目前我的json生成以下矩阵:

"VpcSecurityGroup": {
  "Type": "AWS::EC2::SecurityGroup",
  "Properties": {
    "GroupDescription": "DEV VPC Security group form",
    "SecurityGroupEgress": [
      {
        "CidrIp": "0.0.0.0/0",
        "FromPort": "443",
        "ToPort": "443",
        "IpProtocol": "tcp"
      },
      {
        "CidrIp": "0.0.0.0/0",
        "FromPort": "80",
        "ToPort": "80",
        "IpProtocol": "tcp"
      }
    ],
    "SecurityGroupIngress": [
      {
        "CidrIp": "0.0.0.0/0",
        "FromPort": "443",
        "ToPort": "443",
        "IpProtocol": "tcp"
      },
      {
        "CidrIp": "0.0.0.0/0",
        "FromPort": "80",
        "ToPort": "80",
        "IpProtocol": "tcp"
      }
    ],
    "Tags": [
      {
        "Key": "CostCenter",
        "Value": "0000000000000000"
      },
      {
        "Key": "Application",
        "Value": "Appname"
      },
      {
        "Key": "Function",
        "Value": "cache"
      },
      {
        "Key": "Environment",
        "Value": "dev"
      }
    ],
    "VpcId": "vpc-8c3113e2"
  }
},
"SecurityGroupEgress": {
  "Type": "AWS::EC2::SecurityGroupEgress",
  "Properties": {
    "DestinationSecurityGroupId": {
      "Fn::GetAtt": [
        "VpcSecurityGroup",
        "GroupId"
      ]
    },
    "FromPort": "6379",
    "ToPort": "6379",
    "IpProtocol": "tcp"
  }
},
"SecurityGroupIngress": {
  "Type": "AWS::EC2::SecurityGroupIngress",
  "Properties": {
    "SourceSecurityGroupId": {
      "Fn::GetAtt": [
        "VpcSecurityGroup",
        "GroupId"
      ]
    },
    "FromPort": "6379",
    "ToPort": "6379",
    "IpProtocol": "tcp"
  }
}
[...etc...]

生成此JSON PHP的代码如下:

{"success":1,"message":[{"pedidos_id":"1","pedidos_nome":"Teste","pedidos_email":"ygormagrii@gmail.com"},{"pedidos_id":"19","pedidos_nome":"5","pedidos_email":"camiseta"},{"pedidos_id":"75","pedidos_nome":"6","pedidos_email":"hdj"},{"pedidos_id":"76","pedidos_nome":"5","pedidos_email":"grhd"},{"pedidos_id":"77","pedidos_nome":"5","pedidos_email":"gdf"}]}

我的活动收到了json并试图限制它:

    $user_id=$_REQUEST['user_id'];

$r=mysql_query("select * from tbl_storefinder_pedidos where user_id='$user_id'",$con);

$response= array();
$info=array();
$flag = array();

if( mysql_num_rows( $r ) > 0 ) {
    while($row = mysql_fetch_array($r))
    {
        $flag[pedidos_id]=$row[pedidos_id];
        $flag[pedidos_nome]=$row[pedidos_nome];
        $flag[pedidos_email]=$row[pedidos_email];
        array_push($info, $flag);
    }
        $response["success"] = 1;
        $response["message"] = $info;
        echo json_encode($response);
}
else
{
        $response["success"] = 0;
        $response["message"] = "No entries yet";
        echo json_encode($response);
}
mysql_close($con);

应用程序正常运行,问题仅出现在始终为“NULL”的数据列表中。

2 个答案:

答案 0 :(得分:0)

在doInBackground()中只返回结果而不是返回user_id

答案 1 :(得分:0)

  protected String doInBackground(String... arg0) {

        UserAccessSession userAccess = UserAccessSession.getInstance(FinalizarPedido.this);
        UserSession userSession = userAccess.getUserSession();
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("user_id", String.valueOf(userSession.getUser_id())));

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://marketingdigitalabc.com.br/buysell/pedidos_show.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
                    Toast.LENGTH_LONG).show();
        }

        try {
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.e("pass 2", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 2", e.toString());
        }

        try {

            JSONObject json_data = new JSONObject(result);
            JSONArray arr =  json_data.getJSONArray("message");
            for (int i=0; i < arr.length(); i++) {
                JSONObject  json_dat = arr.getJSONObject(i);

                pedidos_nome.add((json_dat.getString("pedidos_nome")));
                pedidos_id.add((json_dat.getString("pedidos_id")));
                pedidos_email.add((json_dat.getString("pedidos_email")));
            }


        } catch (Exception e) {
            Log.e("Fail 3", e.toString());
        }
        return user_id;

    }