发送JSON对象到HTTP服务器的android [英] sending json object to HTTP server in android

查看:92
本文介绍了发送JSON对象到HTTP服务器的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下code发送JSON对象到一个HTTP服务器。

I am sending a JSON object to a HTTP Server by using the following code.

最主要的是,我还发布尔值。

The main thing is that I have to send Boolean values also.

public void getServerData() throws JSONException, ClientProtocolException, IOException {

    ArrayList<String> stringData = new ArrayList<String>();
    DefaultHttpClient httpClient = new DefaultHttpClient();
    ResponseHandler <String> resonseHandler = new BasicResponseHandler();
    HttpPost postMethod = new HttpPost("http://consulting.for-the.biz/TicketMasterDev/TicketService.svc/SaveCustomer");

    JSONObject json = new JSONObject();
    json.put("AlertEmail",true);
    json.put("APIKey","abc123456789");
    json.put("Id",0);
    json.put("Phone",number.getText().toString());
    json.put("Name",name.getText().toString());
    json.put("Email",email.getText().toString());
    json.put("AlertPhone",false);      
    postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
    String response = httpClient.execute(postMethod,resonseHandler);
    Log.e("response :", response);
    }

,但其示出了线的例外

but its showing the exception in the line

String response = httpClient.execute(postMethod,resonseHandler);

org.apache.http.client.HttpResponseException: Bad Request

任何一个可以帮助我。

can any one help me.

推荐答案

该错误的请求在服务器说,它不喜欢的东西,在您的文章。

The Bad Request is the server saying that it doesn't like something in your POST.

唯一明显的问题,我可以看到的是,你不告诉,你在发送JSON的服务器,所以你可能需要设置一个Content-Type头,表示身体应用程序/ JSON

The only obvious problem that I can see is that you're not telling the server that you're sending it JSON, so you may need to set a Content-Type header to indicate that the body is application/json:

postMethod.setHeader( "Content-Type", "application/json" );

如果不工作,你可能需要看看服务器日志,看看它为什么不喜欢你的文章。

If that doesn't work, you may need to look at the server logs to see why it doesn't like your POST.

如果你没有直接访问服务器日志,那么你需要联络该服务器的所有者尝试和调试的东西。这可能是你的JSON格式稍有不当,有一个必填字段丢失,或者其他类似的问题。

If you don't have direct access to the server logs, then you need to liaise with the owner of the server to try and debug things. It could be that the format of your JSON is slightly wrong, there's a required field missing, or some other such problem.

如果您将无法访问使用该服务器的所有者,您可以尝试使用数据包嗅探器,如Wireshark的,既从您的应用程序,并从成功捕捉后的数据包,并比较这两个尝试制定出有什么不同。这可能是一个有点像寻找一个大海捞针,虽然,特别是对大型机构。

If you can't get access use to the owner of the server, the you could try using a packet sniffer, such as WireShark, to capture packets both from your app, and from a successful POST and compare the two to try and work out what is different. This can be a little bit like finding a needle in a haystack though, particularly for large bodies.

如果你不能得到一个成功的POST的例子,那么你pretty的良好酿,你有没有参考点。

If you can't get an example of a successful POST, then you're pretty well stuffed, as you have no point of reference.

这篇关于发送JSON对象到HTTP服务器的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆