相关疑难解决方法(0)

如何用json编码体进行httpPost调用?

我正在使用以下代码进行httpPost调用,但是当我尝试在chrome扩展中的"简单休息客户端"中提供以下参数时,它返回了400个错误的请求它可以正常工作任何人指导我在这里做了什么错误?

简单休息客户端我输入以下内容:

URL:http://jon2012.com/api/register 方法:POST标题:没有标题,因为它们不是必需的数据:{"email":"test@example.com","first_name":"名称"}在此输入图像描述

Android代码:

HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
        HttpResponse response;
        JSONObject json = new JSONObject();
        try{
            HttpPost post = new HttpPost(url);
            json.put("email", email);
            json.put("first_name", name);
            StringEntity se = new StringEntity( "JSON: " + json.toString());  
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(se);
            response = client.execute(post);
            /*Checking response */
            /*if(response!=null){
                InputStream in = response.getEntity().getContent(); //Get the data in the entity
*/
            int statusCode = response.getStatusLine().getStatusCode();

        }
        catch(Exception e){
            e.printStackTrace();
           // createDialog("Error", "Cannot Estabilish Connection");
        }
Run Code Online (Sandbox Code Playgroud)

任何帮助都会得到满足

android http-post

10
推荐指数
1
解决办法
5万
查看次数

标签 统计

android ×1

http-post ×1