2012-12-06 69 views
1

我已經創建了一個HTTP GET函數,它從我的服務器中檢索響應,並在textview中將其顯示爲JSON。解析爲textview的JSON Android

如何將以下內容轉換爲可由我的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()); 
     } 
    } 
} 
+0

您在這裏有很多答案。你可以讓所有的東西滿足你的需求。 – intrepidkarthi

+0

@Lyan Rai如果我的回答是幫助你,那麼請接受它。 –

回答

2

使用解析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(); 
} 
+0

嗨,如果我已經創建了一個http請求,我該如何實現這一點。 –

+0

@LyanRai然後發佈您的代碼。 –

+0

我已經更新了我的原始代碼。 –

0

我想這是對子級類似:

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/

+0

嗨,如果我已經創建了一個http請求,我該如何實現這一點。 –

0

使用的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; 
} 
} 
0

試試這個:下面的代碼

try { 
      JSONObject jsonObject = new JSONObject(yourString); 
      String response = jsonObject.getString("response"); 
      textView.setText(response); 

    } catch (JSONException e) { 

     e.printStackTrace(); 
    }