import org.json.JSONObject;
constructing the String that you want to send
JSONObject param=new JSONObject();
JSONObject post=new JSONObject();
IM 使用两个对象,因为您可以在另一个对象中包含一个 jsonObject
post.put("username(here i write the key)","someusername"(here i put the value);
post.put("message","this is a sweet message");
post.put("image","http://localhost/someimage.jpg");
post.put("time": "present time");
然后我把 Json 柱放在另一个里面,就像这样
param.put("post",post);
这是我用来提出请求的方法
makeRequest(param.toString());
public JSONObject makeRequest(String param)
{
try
{
dataOutputStream = new DataOutputStream(connection.getOutputStream());
我用这个在日志中看到我发送什么
Log.d("OUTPUT STREAM " ,param);
dataOutputStream.writeBytes(param);
dataOutputStream.flush();
dataOutputStream.close();
InputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
result = new StringBuilder();
String line;
这里构造了字符串
while ((line = reader.readLine()) != null)
{
result.append(line);
}