2012-12-14 40 views
1

Image如何從Android中的JSON網址解析圖像?

我從Wordpress博客解析這個內容。我不知道如何從JSON獲得圖片。這是圖像URL "content":"<p><img class=\"aligncenter\" style=\"cursor: -moz-zoom-in;\" src=\"http:\/\/sphotos-h.ak.fbcdn.net\/hphotos-ak-ash4\/395050_10151219612828815_5123523_n.jpg\" alt=\"http:\/\/sphotos-h.ak.fbcdn.net\/hphotos-ak-ash4\/395050_10151219612828815_5123523_n.jpg\" width=\"390\" height=\"466\" \/><\/p>\n<p><span id=\"more-5267\"><\/span><\/p>\n<p>Some texts here...XXXXXYYYYYZZZZ"

HttpClient client; 
HttpGet get; 
HttpResponse res; 
HttpEntity ent; 
Button b; 
TextView tv1,tv2,tv3; 
@TargetApi(Build.VERSION_CODES.GINGERBREAD) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Typeface tp=Typeface.createFromAsset(getAssets(), "AftaSansThin-Regular.otf"); 
    StrictMode.enableDefaults(); 
    b = (Button) findViewById(R.id.button1); 
    tv1 = (TextView) findViewById(R.id.textView1); 
    tv2 = (TextView) findViewById(R.id.textView2); 
    tv3 = (TextView) findViewById(R.id.textView3); 
    tv1.setTypeface(tp); 
    tv2.setTypeface(tp); 
    tv3.setTypeface(tp); 
    client = new DefaultHttpClient(); 
    get = new HttpGet("http://example.com"); 
    b.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      try { 
       res=client.execute(get); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      ent=res.getEntity(); 
      InputStream is = null; 
      try { 
       is=ent.getContent(); 
      } catch (IllegalStateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
      StringBuffer sb = new StringBuffer(); 
      String line = null; 
      do{ 
       try { 
        line = br.readLine(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       sb.append(line); 
      } while (line!=null); 
      String str = sb.toString(); 

      try { 
       JSONObject ob1 = new JSONObject(str); 
       JSONObject ob2 = ob1.getJSONObject("post"); 
       String title = ob2.getString("title"); 
       String date = ob2.getString("date"); 
       String content = ob2.getString("content"); 



       tv1.setText(title); 
       tv2.setText(date); 
       Spanned marked_up = Html.fromHtml(content); 
       tv3.setText(marked_up.toString(),BufferType.SPANNABLE); 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 
+0

向我們展示您的代碼。 – GreyBeardedGeek

+0

嘗試只從圖像url做json字符串下載,這樣你可以插入一個asynctask並下載所有圖像 – kabuto178

+0

@GreyBeardedGeek檢查代碼! – DroidLearner

回答

1

首先,這不是一個有效的圖片網址 - \ 「HTTP://sphotos-h.ak.fbcdn.net/hphotos-ak-ash4/395050_10151219612828815_5123523_n.jpg \」是更接近你想要的,但仍然有所有的逃生字符。

如果您可以讓JSON數據包將URL作爲字符串發送,效率會更高。如果沒有對其進行配置,以至於它不能逃過所有的字符,那麼就從返回的文本中去掉Url。

接下來,我要做的是在AsyncTask中啓動一個http連接並獲取一個輸入流。使用BitmapFactory.decodeStream(HttpInputStream)將流轉換爲位圖,然後將位圖返回到onPostExecute的UI線程。

如果你需要處理很多圖片下載,你可能要考慮一個開源庫或者寫一個AsyncImageLoader

希望這有助於:)

0

卻很少的內容是JSON。這個內容中的「關鍵」被命名爲「內容」,並且該值是第一個冒號':'後的剩餘部分。其餘的部分是普通的HTML(需要用正則表達式或其他HTML解析器拉出)。

在JavaScript中,你能做的最好的是:

var text = JSON.parse(content)["content"]; // which gives you everything after the colon. 

但你仍然需要一個正則表達式退出時,所有在href標籤:

下面是在命令行的例子:

bash$ content="<p><img class=\"aligncenter\" style=\"cursor: -moz-zoom-in;\" src=\"http:\/\/sphotos-h.ak.fbcdn.net\/hphotos-ak-ash4\/395050_10151219612828815_5123523_n.jpg\" alt=\"http:\/\/sphotos-h.ak.fbcdn.net\/hphotos-ak-ash4\/395050_10151219612828815_5123523_n.jpg\" width=\"390\" height=\"466\" \/><\/p>\n<p><span id=\"more-5267\"><\/span><\/p>\n<p>Some texts here...XXXXXYYYYYZZZZ" 

bash$ echo $content | grep -oE "(src|alt)=\"[^\"]+\.jpg\"" | sed "s/\(src=\|alt=\|\"\)//g" | sed "s/\\///g" 
http:\\sphotos-h.ak.fbcdn.net\hphotos-ak-ash4\395050_10151219612828815_5123523_n.jpg 
http:\\sphotos-h.ak.fbcdn.net\hphotos-ak-ash4\395050_10151219612828815_5123523_n.jpg 

您可以將該正則表達式轉換爲您正在使用的任何語言。

+0

謝謝。但我不知道如何在Android中執行此操作。 – DroidLearner

+0

不知道該怎麼做,到底是什麼? –

+0

@hinesmr可能使用grep,sed :-)嘗試保留相關技術/語言中的代碼片段作爲問題所需。 –

1

如果你已經得到的圖像的URL,您可以使用此LIB從主線程加載圖像異步,我只是嘗試,它是非常容易使用和工作正常,希望它可以幫助你:https://github.com/nostra13/Android-Universal-Image-Loader

+0

將檢查此.. – DroidLearner

+0

如果此人有圖片網址,那麼他可以使用這個庫,否則不會。 –

+0

@Paresh Mayani:當然,如果沒有圖片網址,他無法從服務器顯示圖片。 – VAdaihiep