使用URLConnection将列表<object>从android传递给php以json格式传递给php

时间:2016-04-14 19:46:39

标签: php android json

我正在尝试发送2个变量(mobile_no,group_id)和1个名为&amp;的JSONObject列表。数字参数。 不知何故,我能够在toast中显示mobile_no,group_id,但无法显示列表对象中的任何值。相反,我得到一些错误:PHP错误消息

这是我的代码:

      private Context context;

public SendJsonObject(Context context) {
    this.context = context;
}

@Override
protected String doInBackground(String... params) {
    URL url;
    URLConnection urlConn;
    DataOutputStream printout;

    try {
        url = new URL (php_url);

        urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type","application/json");
        urlConn.connect();
//Create JSONObject here

        List<JSONObject> mylist=new ArrayList<>();

        JSONObject contacts=new JSONObject();
        contacts.put("name","ABC");
        contacts.put("number","9999999999");
        mylist.add(contacts);


        JSONObject jsonParam = new JSONObject();
        jsonParam.put("mobile_no", "998888888");
        jsonParam.put("group_id", "11");
        jsonParam.put("mylist", mylist);

        printout=new DataOutputStream(urlConn.getOutputStream());
        printout.writeBytes(jsonParam.toString());
        printout.flush();
        printout.close();

        BufferedReader reader=new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
        String response= reader.readLine();


        String name=response.substring(3);
        return response;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

protected void onPostExecute(String response) {

    Toast.makeText(context, response, Toast.LENGTH_LONG).show();
    }

}

这是我的PHP代码:

<?php

 $json = file_get_contents('php://input');
 $data = json_decode($json, true);

 echo $data['mobile_no'];
 echo $data['group_id'];
 echo $data['mylist'][0]['name']; 

 ?>

请帮助我这些家伙,真的很感激..... ...

1 个答案:

答案 0 :(得分:2)

我说问题是你正在混合JSON和一个ArrayList,你的php一无所知。最好使用JSONArray代替,这是一个格式正确的JSONObjects数组。它们与JSONObjects非常相似。基本格式如下:

JSONArray jsonArray = new JSONArray();
JSONObject contacts = new JSONObject();
contacts.put("name", "ABC");
contacts.put("number", "9999999999");
jsonArray.put(contacts);

然后你可以将JSONArray添加到你的jsonParam对象中,php应该将它全部识别为JSON。

对评论的回应

使用Google的Gson库,将自定义对象的ArrayList转换为json非常容易。如果您有一个名为MyUser的自定义类:

,请按照以下步骤操作

如果你有一个看起来像这样的课......

class MyUser {
    String name;
    String location;
}

你可以把它变成这样的JSON ......

ArrayList<MyUser> users = new ArrayList<>();

MyUser user1 = new MyUser();
user1.name = "stack";
user1.location = "overflow";
users.add(user1);

Gson gson = new Gson();
String usersJson = gson.toJson(users);
//send the usersJson String into your server

正如您所看到的,使用Gson将自定义对象的ArrayList转换为JSON只需要2行代码。这非常简单。

您可以在此处找到有关在项目中下载和安装Gson的更多信息:https://github.com/google/gson/blob/master/UserGuide.md