从android发布url来检索数据

时间:2012-05-14 07:01:08

标签: java android android-intent

我有一个网址“http://184.82.158.234/~store/rest/system/connect.json”,并将此网址发布为mozilla插件,海报以json的形式返回数据 我想要的是从android发布这个url以将json数据导入到orroids视图中。

任何帮助都非常感谢 感谢

3 个答案:

答案 0 :(得分: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)

检查以下代码:试试这可能对您有帮助。

    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 String。解析json并在任何控件中显示。我在文本视图中显示了这一点,见下文。

final MyProgressDialog progDailog = new MyProgressDialog(Cheking_Review.this);
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (Name.length() > 0 && Name != null) {
                    txtvenue.setText(Name);
                } else {
                    txtvenue.setText(venue_name);
                }
            }
        };

        new Thread() {

            public void run() {
                try {

// put your result here
                    JSONObject jObject = new JSONObject(result);
                    JSONObject menuObject = jObject.getJSONObject("response");
                    JSONObject venueObject = menuObject.getJSONObject("venue");
                    Name = venueObject.getString("name");

                    String id = venueObject.getString("id");

                    Log.d("--------name---------", Name);
                    Log.d("--------id---------", id);

                } catch (Exception e) {
                }
                handler.sendEmptyMessage(0);
                progDailog.dismiss();
            }
        }.start();

答案 2 :(得分: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(); 
}