2012-05-14 126 views
0

我有一個網址「http://184.82.158.234/~store/rest/system/connect.json」和發佈此網址與mozilla插件稱爲海報返回數據的形式json 我想要的是從Android發佈此網址以將該json數據轉換爲android視圖。從Android發佈URL來檢索數據

任何幫助,高度讚賞 感謝

回答

1
public void postData() { 
// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://184.82.158.234/~store/rest/system/connect.json"); 

try { 
    // Add your data 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    // Execute HTTP Post Request 
    HttpResponse response = httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
} 
} 

響應變量將包含您的JSON數據。

+1

的兄弟,我已經嘗試過烏拉圭回合的解決方案不工作的感謝答覆 –

+1

和U可以PLZ告訴我Ÿ我不得不這樣做 nameValuePairs.add(新BasicNameValuePair(「ID」,「12345」)); 因爲我的網址不採取任何參數它只是返回數據沒有任何參數 –

+1

你不能解析json嗎?你可以擴大你的問題多一點 –

1

查看下面的代碼:試試這可能會幫助你。

ArrayList nameValuePairs1 = new ArrayList(); 

     nameValuePairs1.add(new BasicNameValuePair("user_id", "")); 
     nameValuePairs1.add(new BasicNameValuePair("product_id", "")); 
     nameValuePairs1.add(new BasicNameValuePair("product_review",""+text)); 

     HttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httppost = new HttpPost(URL); 

     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs1)); 

     HttpResponse responce = httpclient.execute(httppost); 

     HttpEntity entity = responce.getEntity(); 

     is = entity.getContent(); 

     BufferedReader bufr = new BufferedReader(new InputStreamReader(is1,"iso-8859-1"), 8); 

     StringBuilder sb = new StringBuilder(); 

     sb.append(bufr.readLine() + "\n"); 

     String line = "0"; 

     while ((line = bufr.readLine()) != null) 

     { 

     sb.append(line + "\n"); 

     } 

     is1.close(); 

     result = sb.toString(); 

結果是一個json字符串。解析該json並在任何控件中顯示。我在文本視圖中顯示了這一點,見下文。

+1

好吧,讓我試試 你可以告訴我這是什麼?代碼 nameValuePairs1.add(new BasicNameValuePair(「user_id」,「」)); nameValuePairs1.add(new BasicNameValuePair(「product_id」,「」)); nameValuePairs1.add(new BasicNameValuePair(「product_review」,「」+ text));'code' 因爲我得到的響應沒有任何參數 –

+1

這是你必須發送的參數, www.xyz.com?name=dhaval&product_id=1&product_review=4。這是get方法,但如果你想使用post方法,那麼你必須傳遞數組中的數據,就像我在使用nameValuePairs1數組的答案中寫的一樣。 –

+0

*** *** 05-14 17:35:50.541:E/AndroidRuntime(1216):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.post/com.post.PostActivity}:android.os.NetworkOnMainThreadException * * 這是讓我的應用程序崩潰,我不知道如何解決它 幫助plzzz –

1

這裏是一個函數,也許你可以用來發佈一個字符串到一個URL。

public String doHttpPost(final String fullUrl, final String body) { 

     final URL url = new URL(fullUrl); 

     final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

     // set the request mode as POST 
     urlConnection.setRequestMethod("POST"); 

     urlConnection.setUseCaches(false); 
     urlConnection.setDoOutput(true); 

     urlConnection.setRequestProperty("Accept-charset", "utf-8"); 
     urlConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); 

     final DataOutputStream request = new DataOutputStream(urlConnection.getOutputStream()); 

     // write the body. 
     request.writeBytes(body); 

     // flush output buffer 
     request.flush(); 
     request.close(); 

     // construct a read using input stream and charset. 
     final InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream(), CHARSET_UTF8); 
     final BufferedReader in = new BufferedReader(isr); 

     String inputLine; 
     final StringBuilder stringBuilder = new StringBuilder(); 
     while ((inputLine = in.readLine()) != null) { 
      stringBuilder.append(inputLine).append("\n"); 
     } 

     in.close(); 
     isr.close(); 

     urlConnection.disconnect(); 

     return stringBuilder.toString(); 
}