【问题标题】:JSON parsing into textview AndroidJSON解析成textview Android
【发布时间】:2012-11-24 12:47:21
【问题描述】:

我创建了一个 HTTP GET 函数,该函数从我的服务器检索响应并将其显示为 JSON 在文本视图中。

如何将以下内容变成一个字符串以供我的 textview 读取。

{"response":"ok"}

这是我的 HTTP GET 请求

public class GetMethodEx {

public String getInternetData() throws Exception {


BufferedReader in = null;
String data = null;

try {
    HttpClient client = new DefaultHttpClient();
    client.getConnectionManager().getSchemeRegistry().register(getMockedScheme());

    URI website = new URI("https://server.com:8443/login?username=hm&password=123");
    HttpGet request = new HttpGet();
    request.setURI(website);
    HttpResponse response = client.execute(request);
    response.getStatusLine().getStatusCode();

    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer sb = new StringBuffer("");
    String l = "";
    String nl = System.getProperty("line.separator");
    while ((l = in.readLine()) != null) {
        sb.append(l + nl);
    }
    in.close();
    data = sb.toString();
    return data;
} finally {
    if (in != null) {
        try {
            in.close();
            return data;
        } catch (Exception e) {
            Log.e("GetMethodEx", e.getMessage());
        }
    }
}

【问题讨论】:

  • 这里有很多答案。你可以让一切都满足你的需要。
  • @Lyan Rai 如果我的回答对您有帮助,请接受。

标签: android json http text-parsing


【解决方案1】:

使用以下代码解析 json 数据。

JSONObject jObject;
try {
    jObject = new JSONObject(data);
    String mResponse = jObject.getString("response");
    mTxtView1.setText(mResponse);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

【讨论】:

  • 嗨,如果我已经创建了一个 http 请求,我该如何实现呢。
  • 感谢您的帮助。快速提问,这会出现在我在 oncreate 的主要活动中吗?还是我的 GetMethod 类?
  • @LyanRai 你可以在调用 getInternetData() 方法后将此代码写入 onCreate() 中。
【解决方案2】:

我猜应该是这样的:

String json = "{\"response\":\"ok\"}";
JSONObject object = new JSONObject(json);
String response = object.getString("response");
TextView view = findViewById(id);
view.setText(response);

查看http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

【讨论】:

  • 嗨,如果我已经创建了一个 http 请求,我该如何实现呢。
【解决方案3】:

使用 Json 解析器。

 //json parser for http get
 JSONParser jParser = new JSONParser();
 // getting JSON string from URL
 JSONObject json = jParser.getJSONFromUrl(url);
 String result = json.optString("response", "default");

JSONParser.java

public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}

public JSONObject getJSONFromUrl(String url) {
    // Making HTTP request
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(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 {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
 }
 }

【讨论】:

    【解决方案4】:

    试试这个:

        try {
                JSONObject jsonObject = new JSONObject(yourString);
                String response = jsonObject.getString("response");
                textView.setText(response);
    
        } catch (JSONException e) {
    
            e.printStackTrace();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      相关资源
      最近更新 更多