Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 发送带有参数的JSON对象_Android_Json - Fatal编程技术网

Android 发送带有参数的JSON对象

Android 发送带有参数的JSON对象,android,json,Android,Json,在我的应用程序中,我试图发送带有UserId等参数的JSONObject,是否有人可以向我推荐一些教程,我可以从中获得一些帮助 我得到了这段代码,但在这段代码中,我只能发送JSONObject,而不能发送参数 我的url如下所示: 如何将带有用户ID的JSONObject传递给服务器 public class HTTPPoster { public HttpResponse doPost(String url, JSONObject jsonProfilo) throws ClientP

在我的应用程序中,我试图发送带有UserId等参数的JSONObject,是否有人可以向我推荐一些教程,我可以从中获得一些帮助

我得到了这段代码,但在这段代码中,我只能发送JSONObject,而不能发送参数

我的url如下所示:

如何将带有用户ID的JSONObject传递给服务器

public class HTTPPoster {
    public HttpResponse doPost(String url, JSONObject jsonProfilo) throws ClientProtocolException, IOException {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost request = new HttpPost(url);
        HttpEntity entity;
        StringEntity s = new StringEntity(jsonProfilo.toString());
        s.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        entity = s;
        request.setEntity(entity);
        HttpResponse response;
        response = httpclient.execute(request);
        return response;
    }
}

谢谢..

你说的“我只能发送JSONObject而不是参数”是什么意思?你可以把所有参数都放在JSONObject本身,我不知道你真正的问题是什么在JSONObject中你可以保留你的参数,比如:

JSONObject jsonvalue =new JSONObject();
jsonvalue.put("userId", "urUserId");
示例代码,我在这里发送服务器imeiNumber、userName和paasword:

JSONObject jsonvalue =new JSONObject();
jsonvalue = new JSONObject();
jsonvalue.put("imeiNumber", "00000000000");
jsonvalue.put("userName", mUserName);
jsonvalue.put("plainPassword", mPassword);
我的观点是,你可以向服务器发送userId和globalId

现在在您的HttpConnection中:

HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setChunkedStreamingMode(0);
conn.setUseCaches(false);
String urlParameters = "body"
    + URLEncoder.encode(jsonvalue.toString(), "UTF-8");       
conn.setRequestProperty("Content-Length",
    "" + Integer.toString(urlParameters.getBytes().length));

try {
     OutputStream os = conn.getOutputStream();
     os.write(jsonvalue.toString().getBytes());
     os.close();
} catch (IOException e) {
    e.printStackTrace();
}

将我的用户ID放在JSONObject中可以吗?因为我的URL是这样的:www.example.com/api/JSONObject/userIdofcourse你可以在你的json中保留用户ID和其他细节……它会很好地工作。我和我的php开发人员谈过,他说我需要将用户ID作为字符串分开,而不是jsonobjectIn JSONObject,他可以以字符串的形式获取用户ID……我不明白这一点……你能详细说明一下wtz吗单独发送用户ID的目的是什么??这是我的链接,在这里我必须在userId my id和otherId中插入josn my jsonobject中的值我必须传递一个全局id,请在这里签出,userId是17,otherId是95