如何在Android中使用HTTPClient发送JSON格式的POST请求?

112

我正在尝试使用HTTPClient从Android中POST JSON。我已经在网上找到了很多示例,但是没有一个能够正常工作。我相信这是因为我缺乏JSON /网络知识。我知道有很多示例,但是否有人可以指向一个实际的教程?我正在寻找一个逐步的过程,其中包含代码和解释每个步骤的原因或该步骤的作用。它不需要很复杂,简单就可以。

再次强调,我知道有很多示例,我只是想要一个带有详细说明的示例,明确说明正在发生的事情以及为什么以那种方式进行操作。

如果有人知道一本关于这方面的好书,请告诉我。

再次感谢@terrance的帮助,以下是我所描述的代码:

public void shNameVerParams() throws Exception{
     String path = //removed
     HashMap  params = new HashMap();

     params.put(new String("Name"), "Value"); 
     params.put(new String("Name"), "Value");

     try {
        HttpClient.SendHttpPost(path, params);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

也许你可以发布一个你无法运行的示例吗?通过使其正常工作,您将了解如何将这些部分组合在一起。 - fredw
可能是重复的问题:如何在Android中使用请求发送JSON对象? - Jim G.
5个回答

157

在这个答案中,我使用了由Justin Grammens发布的示例

关于JSON

JSON代表JavaScript对象表示法。 在JavaScript中,可以像object1.nameobject['name']这样引用属性。 本文的示例使用了以下JSON代码。

组成部分
一个以电子邮件为键,foo@bar.com为值的粉丝对象

{
  fan:
    {
      email : 'foo@bar.com'
    }
}

因此,对象等效的写法是fan.email;fan['email'];。这两种写法都具有相同的值'foo@bar.com'

关于HttpClient请求

以下是作者用来进行HttpClient请求的方式。我并不是专家,如果有人能更好地描述一些术语,请随意提出。

public static HttpResponse makeRequest(String path, Map params) throws Exception 
{
    //instantiates httpclient to make request
    DefaultHttpClient httpclient = new DefaultHttpClient();

    //url with the post data
    HttpPost httpost = new HttpPost(path);

    //convert parameters into JSON object
    JSONObject holder = getJsonObjectFromMap(params);

    //passes the results to a string builder/entity
    StringEntity se = new StringEntity(holder.toString());

    //sets the post request as the resulting string
    httpost.setEntity(se);
    //sets a request header so the page receving the request
    //will know what to do with it
    httpost.setHeader("Accept", "application/json");
    httpost.setHeader("Content-type", "application/json");

    //Handles what is returned from the page 
    ResponseHandler responseHandler = new BasicResponseHandler();
    return httpclient.execute(httpost, responseHandler);
}

映射(Map)

如果您对Map数据结构不熟悉,请参阅Java Map参考文档。简单来说,映射类似于字典或哈希表。

private static JSONObject getJsonObjectFromMap(Map params) throws JSONException {

    //all the passed parameters from the post request
    //iterator used to loop through all the parameters
    //passed in the post request
    Iterator iter = params.entrySet().iterator();

    //Stores JSON
    JSONObject holder = new JSONObject();

    //using the earlier example your first entry would get email
    //and the inner while would get the value which would be 'foo@bar.com' 
    //{ fan: { email : 'foo@bar.com' } }

    //While there is another entry
    while (iter.hasNext()) 
    {
        //gets an entry in the params
        Map.Entry pairs = (Map.Entry)iter.next();

        //creates a key for Map
        String key = (String)pairs.getKey();

        //Create a new map
        Map m = (Map)pairs.getValue();   

        //object for storing Json
        JSONObject data = new JSONObject();

        //gets the value
        Iterator iter2 = m.entrySet().iterator();
        while (iter2.hasNext()) 
        {
            Map.Entry pairs2 = (Map.Entry)iter2.next();
            data.put((String)pairs2.getKey(), (String)pairs2.getValue());
        }

        //puts email and 'foo@bar.com'  together in map
        holder.put(key, data);
    }
    return holder;
}

如果您对这篇文章有任何疑问,或者我说的不够清楚,或者您对某些仍然感到困惑的事情没有涉及...等等,随便您想到的东西都可以评论。

(如果Justin Grammens不同意,我会撤下这篇文章。但如果他同意,那么谢谢Justin的支持。)

更新

我刚好收到了一条关于如何使用代码的评论,并且发现返回类型存在错误。方法签名被设置为返回字符串,但在这种情况下并没有返回任何内容。我将签名更改为HttpResponse,并引用此链接“获取HttpResponse的响应体” 路径变量是URL,我已经更新修复了代码中的一个小错误。


谢谢@Terrance。所以在一个不同的类中,他正在创建一个具有不同键和值的映射,稍后将被转换为JSON对象。我尝试实现类似的东西,但我对映射也没有经验,我将尝试将我尝试实现的代码添加到我的原始帖子中。你对正在发生的事情的解释是有道理的,通过创建具有硬编码名称和值的JSONObject,我成功地使其工作。谢谢! - user631063
@pKs 感谢你的提问。我刚刚注意到代码存在问题。 - Terrance
3
getJsonObjectFromMap() 没有必要存在:JSONObject 有一个接受 Map 的构造函数:http://developer.android.com/reference/org/json/JSONObject.html#JSONObject(java.util.Map) - pr1001
如果JSON数据中没有“fan”,那么应该删除什么? - Ben
你应该使用反射来编写Json对象,使用像Google的Gson这样的库,这将为你节省大量代码和头痛。 - Oliver Dixon
显示剩余8条评论

41

这里有一个与 @Terrance 的答案不同的备选方案。你可以轻松地外包转换过程。 Gson库 会将各种数据结构转换为JSON格式,也可以进行反向操作。

public static void execute() {
    Map<String, String> comment = new HashMap<String, String>();
    comment.put("subject", "Using the GSON library");
    comment.put("message", "Using libraries is convenient.");
    String json = new GsonBuilder().create().toJson(comment, Map.class);
    makeRequest("http://192.168.0.1:3000/post/77/comments", json);
}

public static HttpResponse makeRequest(String uri, String json) {
    try {
        HttpPost httpPost = new HttpPost(uri);
        httpPost.setEntity(new StringEntity(json));
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        return new DefaultHttpClient().execute(httpPost);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

可以使用Jackson代替Gson完成类似的操作。我还建议查看Retrofit,它会为您隐藏许多样板代码。对于更有经验的开发人员,我建议尝试RxAndroid


@JJD 到目前为止,您建议将数据发送到远程服务器,这是一个很好的解释,但我想知道如何使用HTTP协议解析JSON对象。您能否详细说明一下您的答案,并包括JSON解析。这对于所有新手来说都非常有帮助。 - AndroidDev
@AndroidDev 请开一个新问题,因为这个问题是关于从客户端向服务器发送数据的。随意在此处放置链接。 - JJD
@JJD 噢,对不起,这段代码必须在单独的线程中调用。 - Konstantin Konopko
您好,如果我的URL是一个JSP页面的URL,那么我该如何在该JSP中获取地图值? - Newinjava
如何获取服务器返回的响应 - Anand Savjani
显示剩余6条评论

34

我建议使用这个HttpURLConnection而不是HttpGet。因为在Android API 22级别之后,HttpGet已经被弃用。

HttpURLConnection httpcon;  
String url = null;
String data = null;
String result = null;
try {
  //Connect
  httpcon = (HttpURLConnection) ((new URL (url).openConnection()));
  httpcon.setDoOutput(true);
  httpcon.setRequestProperty("Content-Type", "application/json");
  httpcon.setRequestProperty("Accept", "application/json");
  httpcon.setRequestMethod("POST");
  httpcon.connect();

  //Write       
  OutputStream os = httpcon.getOutputStream();
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
  writer.write(data);
  writer.close();
  os.close();

  //Read        
  BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream(),"UTF-8"));

  String line = null; 
  StringBuilder sb = new StringBuilder();         

  while ((line = br.readLine()) != null) {  
    sb.append(line); 
  }         

  br.close();  
  result = sb.toString();

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} 

5

这个任务的代码量太多了,你可以尝试使用这个库https://github.com/kodart/Httpzoid。它内部使用GSON并提供与对象一起工作的API,隐藏了所有JSON细节。

Http http = HttpFactory.create(context);
http.get("http://example.com/users")
    .handler(new ResponseHandler<User[]>() {
        @Override
        public void success(User[] users, HttpResponse response) {
        }
    }).execute();

很棒的解决方案,不幸的是这个插件缺乏Gradle支持:/ - electronix384128

3

建立HTTP连接并从RESTFULL Web服务获取数据有几种方法。最近的方法是使用GSON。但在使用GSON之前,您必须对创建HTTP客户端并与远程服务器进行数据通信的最传统方式有一定的了解。我已经提到了使用HTTPClient发送POST和GET请求的两种方法。

/**
 * This method is used to process GET requests to the server.
 * 
 * @param url 
 * @return String
 * @throws IOException
 */
public static String connect(String url) throws IOException {

    HttpGet httpget = new HttpGet(url);
    HttpResponse response;
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used. 
    int timeoutConnection = 60*1000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 60*1000;

    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    HttpClient httpclient = new DefaultHttpClient(httpParameters);
    try {

        response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            result = convertStreamToString(instream);
            //instream.close();
        }
    } 
    catch (ClientProtocolException e) {
        Utilities.showDLog("connect","ClientProtocolException:-"+e);
    } catch (IOException e) {
        Utilities.showDLog("connect","IOException:-"+e); 
    }
    return result;
}


 /**
 * This method is used to send POST requests to the server.
 * 
 * @param URL
 * @param paramenter
 * @return result of server response
 */
static public String postHTPPRequest(String URL, String paramenter) {       

    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used. 
    int timeoutConnection = 60*1000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 60*1000;

    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    HttpClient httpclient = new DefaultHttpClient(httpParameters);
    HttpPost httppost = new HttpPost(URL);
    httppost.setHeader("Content-Type", "application/json");
    try {
        if (paramenter != null) {
            StringEntity tmp = null;
            tmp = new StringEntity(paramenter, "UTF-8");
            httppost.setEntity(tmp);
        }
        HttpResponse httpResponse = null;
        httpResponse = httpclient.execute(httppost);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            InputStream input = null;
            input = entity.getContent();
            String res = convertStreamToString(input);
            return res;
        }
    } 
     catch (Exception e) {
        System.out.print(e.toString());
    }
    return null;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接