0

我有一个网址“http://184.82.158.234/~store/rest/system/connect.json”并使用名为poster的mozilla插件发布此网址返回json形式的数据我想要的是将此网址从android发布到将该 json 数据放入 androids 视图中。

任何帮助都非常感谢

4

3 回答 3

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 数据。

于 2012-05-14T07:07:30.110 回答
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 并显示在任何控件中。我在文本视图中显示了这一点,见下文。

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();
于 2012-05-14T07:12:28.310 回答
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(); 
}
于 2016-05-20T21:21:08.020 回答