且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSONObject必须在字符1 [字符2第1行]处以"{"开头

更新时间:2023-10-25 10:16:40

在我以前的答案中编辑代码并将代码添加到我的先前答案中,当您在Android中进行编码时谁在帮助我,我将添加另一个解决方案,这次使用 http client

Instead edit and adding code to my previous answer who was helping when you coding in Android , i will add another solution , this time using http client

我不会在同一个文件中执行所有操作,但是我创建了两个文件,一个正在解析(可重用),另一个正在使用第一个(客户端请求).

I din't do everything in the same file , but i created two files , one of parsing (reusable) and another one who is using the first one (client request) .

  • JSONParser

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    // constructor
    public JSONParser() {
    }
    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method) {
        // Making HTTP request
        try {
            // check for request method
            if("POST".equals(method)){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);  
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }else if("GET".equals(method)){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
         e.printStackTrace();
        }
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
             e.printStackTrace();
        }
        return jObj; 
    }
}

  • 客户

    import java.io.IOException;
    import java.util.Iterator;
    import org.json.JSONException;
    import org.json.JSONObject;
    public class Client {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                getJsonHttp();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void getJsonHttp() throws IOException, JSONException {
                JSONParser jsp = new JSONParser();
                String url ="http://api.wunderground.com/api/<key>/conditions/q/Australia/Brisbane.json";
                    JSONObject obj = jsp.makeHttpRequest(url, "GET");
                    JSONObject disp = obj.getJSONObject("current_observation");
                   JSONObject des = disp.getJSONObject("display_location");
                   JSONObject jObject = new JSONObject(des.toString().trim());
                   Iterator<?> keys = jObject.keys();
                   while( keys.hasNext() ) {
                       String key = (String)keys.next();
                          if("full".equals(key) || "city".equals(key) || "state".equals(key) || "state_name".equals(key)){
                              System.out.println(jObject.get(key).toString());
                          }
                   }   
        }
    }
    

  • 如果执行代码,则会得到以下结果:

    If you execute the code , you will get this result:

    您应该添加到构建路径中的库是:

    THe libraries you should add to the build path are :

    我从这里下载它: HttpComponents下载