在Android中使用JSON发送请求 [英] send request using json in android

查看:141
本文介绍了在Android中使用JSON发送请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/5141868/send-requst-from-json-to-java-server-in-android">send requst从JSON在Android的 Java服务器

我现在在画廊的项目工作。所有图像将在服务器database.using JSON我想我们正在使用java.when在数据库中更改过的图像发送请求并得到服务器响应,在服务器端,画廊图像也必须改变dynamically.how使用的JSON为此,请帮我

解决方案

 包com.devstream.http;
    进口org.json.JSONException;
    进口org.json.JSONObject;
    进口android.app.Activity;
    进口android.os.Bundle;
    进口android.util.Log;

    公共类MainActivity延伸活动{
     私有静态最后字符串变量=MainActivity;
     私有静态最后字符串的URL =htt​​p://www.yourdomain.com:80;

     @覆盖公共无效的onCreate(包savedInstanceState){
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.main);

      // JSON对象以保存信息,该信息发送到服务器
      JSONObject的jsonObjSend =新的JSONObject();

      尝试 {
       //添加键/值对
       jsonObjSend.put(key_1,值_1);
       jsonObjSend.put(key_2,_2);

       //添加一个嵌套的JSONObject(如头信息)
       JSONObject的标题=新的JSONObject();
       header.put(的devicetype,安卓); // 设备类型
       header.put(deviceVersion,2.0); //设备操作系统版本
       header.put(语言,ES-ES); // Android客户端的语言
       jsonObjSend.put(头,头);

       //输出我们发送给logcat的JSON对象:
       Log.i(TAG,jsonObjSend.toString(2));

      }赶上(JSONException E){
       e.printStackTrace();
      }

      //发送HttpPostRequest和接收的JSONObject的回报
      JSONObject的jsonObjRecv = HttpClient.SendHttpPost(URL,jsonObjSend);

      / *
       *从这里开始做任何你想要与你的JSONObject,例如:
       * 1)获取一个键的值:jso​​nObjRecv.get(钥匙);
       * 2)获取一个嵌套的JSONObject:jsonObjRecv.getJSONObject(钥匙)
       * 3)获取一个嵌套的JSONArray:jsonObjRecv.getJSONArray(钥匙)
       * /


     }
    }
 

Possible Duplicate:
send requst from json to java server in android

now i am working in gallery project. all images will be on the server database.using json i want to send request and get response from server, in server side we are using java.when the images changed in database,gallery images also must be changed dynamically.how to use json for this purpose,please help me

解决方案

package com.devstream.http;  
    import org.json.JSONException;
    import org.json.JSONObject;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;

    public class MainActivity extends Activity {
     private static final String TAG = "MainActivity";
     private static final String URL = "http://www.yourdomain.com:80";

     @Override   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      // JSON object to hold the information, which is sent to the server
      JSONObject jsonObjSend = new JSONObject();

      try {
       // Add key/value pairs
       jsonObjSend.put("key_1", "value_1");
       jsonObjSend.put("key_2", "value_2");

       // Add a nested JSONObject (e.g. for header information)
       JSONObject header = new JSONObject();
       header.put("deviceType","Android"); // Device type
       header.put("deviceVersion","2.0"); // Device OS version
       header.put("language", "es-es"); // Language of the Android client
       jsonObjSend.put("header", header);

       // Output the JSON object we're sending to Logcat:
       Log.i(TAG, jsonObjSend.toString(2));

      } catch (JSONException e) {
       e.printStackTrace();
      }

      // Send the HttpPostRequest and receive a JSONObject in return
      JSONObject jsonObjRecv = HttpClient.SendHttpPost(URL, jsonObjSend);

      /*
       *  From here on do whatever you want with your JSONObject, e.g.
       *  1) Get the value for a key: jsonObjRecv.get("key");
       *  2) Get a nested JSONObject: jsonObjRecv.getJSONObject("key")
       *  3) Get a nested JSONArray: jsonObjRecv.getJSONArray("key")
       */


     }
    }

这篇关于在Android中使用JSON发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆