【问题标题】:How to fetch data using post or get method using an URL如何使用 post 或使用 URL 的 get 方法获取数据
【发布时间】:2016-08-11 09:51:41
【问题描述】:

我需要使用网络服务从this URL 获取值

【问题讨论】:

  • @shivaKumarg plz 在这个 url 上传递哪个参数
  • 使用参数作为getString
  • 欢迎来到 SO。你能告诉我们你到目前为止做了什么吗?
  • 欢迎来到 SO!请提供更详细的说明,说明您正在努力实现的目标以及您迄今为止所做的尝试

标签: android json


【解决方案1】:

当我尝试你的网址时:http://xid.e42.in/testMobile.php

我收到了回复:

【讨论】:

  • 我也得到了这个..我不知道这是不是答案?
  • @shivakumarlg 然后请在这个 web 服务中使 web 服务正确,你没有得到数据
  • @shivakumarlg 制作这种类型的网络服务:lnksocial.com/appservice/…
  • 您使用的是哪个网络服务?
  • @shivakumarlg 我正在使用 php 网络服务
【解决方案2】:

选项 1:- 您可以使用 JSON PARSER 类,例如:-

在 build.gradle 文件中添加这一行 android { useLibrary 'org.apache.http.legacy' }

import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
                                      List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
               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;

    }
}

现在,您可以通过 METHOD GET 或 POST 来使用它,例如:-

List<NameValuePair> params = new ArrayList<NameValuePair>();
 params.add(new BasicNameValuePair("id","1")); //any get or post parameter
        final JSONObject json = jParser.makeHttpRequest("http://xid.e42.in/testMobile.php", "GET", params);

你会得到json对象..你可以通过JSONArray得到你所有的json数据..

选项 2 您可以通过 Android Volley 使用 StringRequest,这很简单,只需 Google 一下即可。

如果您在下面有任何查询评论:) 快乐编码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多