Android 使用http请求解析具有单个对象的url

Android 使用http请求解析具有单个对象的url,android,json,parsing,android-asynctask,Android,Json,Parsing,Android Asynctask,我的url是:{aff_id} 它包含单个对象{“success”:true}。如何解析此url并将json数据存储在变量中 我的工作背景方法: protected Void doInBackground(Void... unused) { String json = ""; URL url; HttpURLConnection connection = null; try { url = new URL("http://a.nextp

我的url是:{aff_id}

它包含单个对象
{“success”:true}
。如何解析此
url
并将
json
数据存储在变量中

我的工作背景方法:

    protected Void doInBackground(Void... unused) {

    String json = "";
    URL url;
    HttpURLConnection connection = null;
    try {
        url =  new URL("http://a.nextput.com/apps/init/4/a/9fe2d2cbaa8332a4633be17b79208181-2y-10-ELVM4HwkaYaCVu6203Zjfus-G/o?aff_id=");
        connection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = connection.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream);
        int data = reader.read();

        while (data != -1) {

            char currentChar = (char) data;
            data = reader.read();
            json += currentChar;


        }

    }catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }

    return json;

    JSONObject jsonObject = new JSONObject(json);
    boolean state = jsonObject.getBoolean("success");
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("state",state);
    editor.commit();
    return null;

}

作为回报,json;它显示不兼容的类型,在JSONObject中显示未处理的异常:org.json.JSONException。如何解决此问题?

以下是此问题的解决方案-

 // Making HTTP request
       InputStream is = null;
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);//YOUR URL

            HttpResponse httpResponse = httpClient.execute(httpPost);
            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, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
        JSONObject  jObj = new JSONObject(json);
        boolean isSuccess = jObj.getBoolean("success");
        System.out.println("success : " + isSuccess);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

有很多json解析的例子……请先尝试一下。@RavindraKushwaha的可能重复项与您提出的问题不同。我尝试过json解析。但这里只有一个对象,那就是响应。我不知道如何在此url中执行解析。请帮忙。你试过什么@himanshutiwari。。。??它使用起来很简单,比如JsonObject JsonObject=newjsonobject(“这是您的响应”);而不是字符串suces=jsonobject.getBoolean(“成功”)@RavindraKushwaha我编辑了这个问题并展示了我的尝试。这给了我们一些错误。我在上一篇文章中也提到了他们。请帮助我解决它们。在您的代码中,它显示无法解决符号“is”InputStream is=null;