求Android通过http请求获取json数据的demo 300C

Android获取http://www.yemaxgo.com/connect.php这个网址上的数据并显示在UI上

10个回答

https://blog.csdn.net/shun_fzll/article/details/16370151
上面这个非常详细~~之前参考过~~

有图有真相效果截图图片说明

源代码下载地址:链接:https://pan.baidu.com/s/1hZ6PAHa0ZwJXOihyQLR1dw 密码:xnl3

代码中只写了id speed pm25 time几个字段,剩下的你看着不全,很简单的一看就明白

目录Csdn0816\app\build\outputs\apk下 apk可以真机运行

这是天气预报吗?我之前有个天气预报的demo 你可以看看也是http请求 解析json 界面展示
链接:https://pan.baidu.com/s/1rqK-cdRY1fFd9mSNaJ_E5g 密码:9fzz

public static void httpRequestDemo(){
try {
URL url=new URL("http://www.yemaxgo.com/connect.php");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
if(connection.getResponseCode()==200){
InputStream is=connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sBuffer=new StringBuffer();
String str=null;
while ((str=br.readLine())!=null) {
sBuffer.append(str);
}
System.out.println(sBuffer.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
}
}

HTTP请求发送JSON数据:

 public class TransObjectToWeb {
    /**
     * 创建方法:将对象传输到 web 端
     * @param user
     */
    private Boolean flag = false;
    public boolean sendToWeb(User user){

        try {
            // (1)服务器的访问路径
            Log.i("TAG","创建连接");
            URL url = new URL("http://192.168.0.28:8080/WinSockWeb/WinSockForAndroid");
            HttpURLConnection http = (HttpURLConnection) url.openConnection();  //实例化连接对象
            http.setDoInput(true);  //可读可写
            http.setDoOutput(true);
            http.setUseCaches(false);  //不允许使用缓存
            http.setRequestMethod("POST");  //设置传输方式为 post
            http.connect();  //创建连接
            Log.i("TAG","创建连接成功");

            // (2)数据写入流发送至服务器
            OutputStream os = http.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            Log.i("TAG","创建json 对象");
            JSONArray jsonArray = new JSONArray();  //创建 json 对象
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("username",user.getUsername());  //写入对象数据
            jsonObject.put("gender",user.getGender());
            jsonObject.put("age",user.getAge());
            jsonArray.put(jsonObject);
            bw.write(jsonArray.toString());
            bw.flush();

            // (3)数据读取流接收数据
            InputStream is = http.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            String result = br.readLine();  //获取web 端返回的数据
            if (result.equals("succeed")) {  //如果返回数据为 succeed 处理成功,否则失败
                flag = true;
            }

            // (4)关闭相关流
            if (os != null)  os.close();
            if (osw != null)  osw.close();
            if (is != null)  is.close();
            if (isr != null)  isr.close();
            if (br != null)  br.close();
            if (bw != null)  bw.close();

        } catch (Exception e){e.printStackTrace();}
        return flag;
    }
}

接受并解析

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");  //设置编码方式
        response.setCharacterEncoding("utf-8");
        System.out.println("接收到请求");

        User user = null;  //初始化接收的传输对象为空

        //(1) 获取android 请求,对传输对象的数据进行处理
        InputStream is = request.getInputStream();
        InputStreamReader isr = new InputStreamReader(is,"utf-8");
        BufferedReader br = new BufferedReader(isr);
        String userjson = br.readLine();

        System.out.println("userjson:"+userjson.toString());  //输出 json 格式数据

        try {
            JSONArray userarr = new JSONArray(userjson);
            JSONObject userobj = userarr.getJSONObject(0);

            user = new User();  //实例化 user 对象,否则报空指针异常
            user.setUsername(userobj.getString("username"));
            user.setGender(userobj.getString("gender"));
            user.setAge(userobj.getInt("age"));

            System.out.println("该传输对象:\n"
                    +"姓名: "+user.getUsername()
                    +"\n性别: "+user.getGender()
                    +"\n年龄: "+user.getAge());

            // (2)返回结果,成功或失败
            OutputStream os = response.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os,"utf-8");
            BufferedWriter bw = new BufferedWriter(osw);


            if (user != null) 
                bw.write("succeed");
            else 
                bw.write("error");

            bw.flush();
            if (os != null)  os.close();
            if (osw != null)  osw.close();
            if (is != null)  is.close();
            if (isr != null)  isr.close();
            if (br != null)  br.close();
            if (bw != null)  bw.close();

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

示例demo

volley+listview+json了解一下

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问