如何检查String Builder的内容

时间:2021-12-17 21:33:36

I am trying to parse some JSON and send it to a EditText field in my android application. I recently discovered that an error I was getting was stemming from the contents of my StringBuilder. Instead of using my StringBuilder I hardcoded a sample of json into a string and sent that to the EditText field. That works fine and I get the value I am looking for to show up. However I need this to work via accessing the API I am using and not hardcoding. Below is my JSON Parser class. Is there a way I can check what is in my stringBuilder after the append before inputStream is closed. This way I can sort that out and return my jSon back to stringBuilder.toString() instead of hardcoding it.

我试图解析一些JSON并将其发送到我的Android应用程序中的EditText字段。我最近发现我得到的错误源于我的StringBuilder的内容。我没有使用我的StringBuilder,而是将json样本硬编码为字符串并将其发送到EditText字段。这很好,我得到了我想要显示的价值。但是,我需要通过访问我正在使用的API而不是硬编码来工作。下面是我的JSON Parser类。有没有办法在inputStream关闭之前追加附加后检查我的stringBuilder中的内容。这样我可以将其排序并将我的jSon返回给stringBuilder.toString()而不是硬编码。

package com.apitest.rottentomatoestest;

import java.io.*;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.*;

import android.util.Log;

public class JSONParser {

static InputStream inputStream = null;
static JSONObject jObject = null;
static String jSon = "";

public JSONParser() {
    // TODO Auto-generated constructor stub
}

public JSONObject getJSONFromUrl(String url){

    //Make HTTP Request
    try {
        //defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        inputStream = httpEntity.getContent();

    } catch (UnsupportedEncodingException e){
        e.printStackTrace();
    } catch (ClientProtocolException e){
        e.printStackTrace();
    }catch (IOException e){
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null){
            stringBuilder.append(line + "\n");
        }
        inputStream.close();

        jSon = "{\"cast\":[{\"id\":\"162661723\",\"name\":\"Snoop Dogg\"}]}";

    } catch (Exception e){
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    //try to parse the string to JSON Object
    try {
        jObject = new JSONObject(jSon);

    } catch (JSONException e){
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    //return JSON String
    return jObject;
}

}

3 个解决方案

#1


0  

You can use:

您可以使用:

Log.d("JSON Contents", stringBuilder.toString());

just before you close the inputStream.

就在关闭inputStream之前。

Update: The 596 Service Not Found message is often caused by an incorrect URL.

更新:596 Service Not Found消息通常由不正确的URL引起。

#2


0  

try doing this instead :

尝试这样做:

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

#3


0  

You can use org.json.JSONObject for parsing JSON into usable Java Object.

您可以使用org.json.JSONObject将JSON解析为可用的Java对象。

#1


0  

You can use:

您可以使用:

Log.d("JSON Contents", stringBuilder.toString());

just before you close the inputStream.

就在关闭inputStream之前。

Update: The 596 Service Not Found message is often caused by an incorrect URL.

更新:596 Service Not Found消息通常由不正确的URL引起。

#2


0  

try doing this instead :

尝试这样做:

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

#3


0  

You can use org.json.JSONObject for parsing JSON into usable Java Object.

您可以使用org.json.JSONObject将JSON解析为可用的Java对象。