如何使用 REST 将 Json 字符串发送到 android 参数中的 java webservice?
Posted
技术标签:
【中文标题】如何使用 REST 将 Json 字符串发送到 android 参数中的 java webservice?【英文标题】:How to send Json String using REST to java webservice in Parameter in android? 【发布时间】:2014-05-21 02:38:00 【问题描述】:恶魔,我将带有三个参数的 JSON 字符串发送到 java web 服务方法。但在 java 端方法无法在控制台中打印。请指导我从下面的代码中改变什么?
String json = "";
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpPost httpPost = new HttpPost(url);
HttpGet httpGet = new HttpGet(url);
JSONObject jsonObject = new JSONObject();
try
jsonObject.put("name", "ghanshyam");
jsonObject.put("country", "India");
jsonObject.put("twitter", "ghahhd");
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
// 6. set httpPost Entity
System.out.println(json);
httpPost.setEntity(se);
httpGet.se
// 7. Set some headers to inform server about the type of the content
//httpPost.addHeader( "SOAPAction", "application/json" );
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
//String s = doGet(url).toString();
Toast.makeText(getApplicationContext(), "Data Sent", Toast.LENGTH_SHORT).show();
【问题讨论】:
如果您发布完整的代码,我们可能会为您提供帮助。但是为什么你同时在 POST 和 GET 中准备请求??? HttpPost httpPost = 新的 HttpPost(url); HttpGet httpGet = new HttpGet(url); 您应该使用 volley 或 retrofit 之类的库来进行无痛 api 调用。 【参考方案1】:使用以下代码将 json 发布到 Java web-service: 并以字符串形式获取响应。
JSONObject json = new JSONObject();
json.put("name", "ghanshyam");
json.put("country", "India");
json.put("twitter", "ghahhd");
HttpPost post = new HttpPost(url);
post.setHeader("Content-type", "application/json");
post.setEntity(new StringEntity(json.toString(), "UTF-8"));
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse httpresponse = client.execute(post);
HttpEntity entity = httpresponse.getEntity();
InputStream stream = entity.getContent();
String result = convertStreamToString(stream);
你的 convertStremToString() 方法如下:
public static String convertStreamToString(InputStream is)
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
catch (IOException e)
e.printStackTrace();
finally
try
is.close();
catch (IOException e)
e.printStackTrace();
return sb.toString();
【讨论】:
先生,您能给我定义一下什么是“客户”吗? 哎呀!!对不起 。 client 是您的 HttpClient 对象。在开头写另一行,例如: DefaultHttpClient client = new DefaultHttpClient(); 兄弟,它在服务器站点上给出了 NoResponseheader 错误。 你如何在服务器端使用参数。密码是什么? 导入 javax.ws.rs.GET;导入 javax.ws.rs.POST;导入 javax.ws.rs.Path;导入 javax.ws.rs.Produces;导入 javax.ws.rs.core.MediaType; (attherate)Path("/GauizzWebServiceForandroid") public class GauizzWebServiceForAndroid (at the rate)GET (at the rate)POST (at the rate)Path("/responsePrint") (at the rate)Produces(MediaType.APPLICATION_JSON) public String responsePrint(String json) String demoString = "prakash"; System.out.println(json);返回演示字符串;以上是关于如何使用 REST 将 Json 字符串发送到 android 参数中的 java webservice?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 HttpClient 将带有 JSON 的 DELETE 发送到 REST API
如何将 json 数据从我的 jsp 页面发送到 spring rest 控制器
将 JSONObject 中的文件发送到 REST WebService
将 JSON 发送到 WCF Rest 服务 - 对象始终为空