Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;

import android.os.Environment;
import android.util.Log;

public class Main {
    public static void server2mobile(Context context, String type) {

        String serverPath = "http://shaunrain.zicp.net/FileUp/QueryServlet?type=" + type;
        try {
            URL url = new URL(serverPath);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(10000);
            conn.setRequestMethod("GET");

            int code = conn.getResponseCode();
            Log.d("code", code + "");
            if (code == 200) {
                InputStream is = conn.getInputStream();

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int len = 0;
                byte[] buffer = new byte[1024];

                while ((len = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                is.close();
                conn.disconnect();

                String result = new String(baos.toByteArray());
                String[] results = result.split("[$]");
                for (String r : results) {
                    if (r.length() != 0) {
                        URL json = new URL(r);
                        HttpURLConnection con = (HttpURLConnection) json.openConnection();
                        con.setConnectTimeout(5000);
                        con.setRequestMethod("GET");
                        int co = con.getResponseCode();
                        if (co == 200) {
                            File jsonFile;
                            if (r.endsWith("clear.json")) {
                                jsonFile = new File(
                                        Environment.getExternalStorageDirectory() + "/traffic_json/clear.json");
                            } else if (r.endsWith("crowd.json")) {
                                jsonFile = new File(
                                        Environment.getExternalStorageDirectory() + "/traffic_json/crowd.json");
                            } else if (r.endsWith("trouble.json")) {
                                jsonFile = new File(
                                        Environment.getExternalStorageDirectory() + "/traffic_json/trouble.json");
                            } else {
                                jsonFile = new File(
                                        Environment.getExternalStorageDirectory() + "/traffic_json/control.json");
                            }

                            if (jsonFile.exists())
                                jsonFile.delete();
                            jsonFile.createNewFile();
                            try (BufferedReader reader = new BufferedReader(
                                    new InputStreamReader(con.getInputStream(), "gb2312"));
                                    BufferedWriter writer = new BufferedWriter(new FileWriter(jsonFile));) {
                                String line = null;
                                while ((line = reader.readLine()) != null) {
                                    writer.write(line);
                                }
                            }

                        }
                        con.disconnect();
                    }
                }

            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}