本文目录一览:
- 1、java怎么接收android请求过来的json数据
- 2、java做手机端后台,如何接受get请求参数
- 3、java后台怎么获取app传过来的数据
- 4、移动端调用的接口在java后台端用webservice的方式怎么写
- 5、请问java web 后台如何接收app端的请求以及返回结果,跪求一个的helloword 版本
- 6、如何用java进行移动端后台开发
java怎么接收android请求过来的json数据
java接收android请求json数据的方法:
如果发送的没有参数名称 你可以直接得到请求体,如
InputStream inputStream = urlConnection.getInputStream();
String encoding = urlConnection.getContentEncoding();
String body = IOUtils.toString(inputStream, encoding);
System.out.println(body);
如果body 就是那个json内容 使用fastjson进行解析就可以了
JSONObject map = JSON.parseObject(body);
System.out.println(map.getString("mobileNo")); //还是System.out.println(map.get("mobileNo")); ?具体看一下接口文档
或者
Map map = JSON.parseObject(body, Map.class);
System.out.println(map.get("mobileNo"));
java做手机端后台,如何接受get请求参数
package com.weixin.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
//import bsh.ParseException;
import com.google.gson.Gson;
/**
* TODO
* @Version 1.0
*/
public class HttpClients {
/** UTF-8 */
private static final String UTF_8 = "UTF-8";
/** 日志记录tag */
private static final String TAG = "HttpClients";
/** 用户host */
private static String proxyHost = "";
/** 用户端口 */
private static int proxyPort = 80;
/** 是否使用用户端口 */
private static boolean useProxy = false;
/** 连接超时 */
private static final int TIMEOUT_CONNECTION = 60000;
/** 读取超时 */
private static final int TIMEOUT_SOCKET = 60000;
/** 重试3次 */
private static final int RETRY_TIME = 3;
/**
* @param url
* @param requestData
* @return
*/
public String doHtmlPost(HttpClient httpClient,HttpPost httpPost )
{
String responseBody = null;
int statusCode = -1;
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
Header lastHeader = httpResponse.getLastHeader("Set-Cookie");
if(null != lastHeader)
{
httpPost.setHeader("cookie", lastHeader.getValue());
}
statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("HTTP" + " " + "HttpMethod failed: " + httpResponse.getStatusLine());
}
InputStream is = httpResponse.getEntity().getContent();
responseBody = getStreamAsString(is, HTTP.UTF_8);
} catch (Exception e) {
// 发生网络异常
e.printStackTrace();
} finally {
// httpClient.getConnectionManager().shutdown();
// httpClient = null;
}
return responseBody;
}
/**
*
* 发起网络请求
*
* @param url
* URL
* @param requestData
* requestData
* @return INPUTSTREAM
* @throws AppException
*/
public static String doPost(String url, String requestData) throws Exception {
String responseBody = null;
HttpPost httpPost = null;
HttpClient httpClient = null;
int statusCode = -1;
int time = 0;
do {
try {
httpPost = new HttpPost(url);
httpClient = getHttpClient();
// 设置HTTP POST请求参数必须用NameValuePair对象
ListBasicNameValuePair params = new ArrayListBasicNameValuePair();
params.add(new BasicNameValuePair("param", requestData));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 设置HTTP POST请求参数
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("HTTP" + " " + "HttpMethod failed: " + httpResponse.getStatusLine());
}
InputStream is = httpResponse.getEntity().getContent();
responseBody = getStreamAsString(is, HTTP.UTF_8);
break;
} catch (UnsupportedEncodingException e) {
time++;
if (time RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
} catch (ClientProtocolException e) {
time++;
if (time RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
} catch (IOException e) {
time++;
if (time RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生网络异常
e.printStackTrace();
} catch (Exception e) {
time++;
if (time RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生网络异常
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
httpClient = null;
}
} while (time RETRY_TIME);
return responseBody;
}
/**
*
* 将InputStream 转化为String
*
* @param stream
* inputstream
* @param charset
* 字符集
* @return
* @throws IOException
*/
private static String getStreamAsString(InputStream stream, String charset) throws IOException {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset), 8192);
StringWriter writer = new StringWriter();
char[] chars = new char[8192];
int count = 0;
while ((count = reader.read(chars)) 0) {
writer.write(chars, 0, count);
}
return writer.toString();
} finally {
if (stream != null) {
stream.close();
}
}
}
/**
* 得到httpClient
*
* @return
*/
public HttpClient getHttpClient1() {
final HttpParams httpParams = new BasicHttpParams();
if (useProxy) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_CONNECTION);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_SOCKET);
HttpClientParams.setRedirecting(httpParams, true);
final String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.14) Gecko/20110218 Firefox/3.6.14";
HttpProtocolParams.setUserAgent(httpParams, userAgent);
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpClientParams.setCookiePolicy(httpParams, CookiePolicy.RFC_2109);
HttpProtocolParams.setUseExpectContinue(httpParams, false);
HttpClient client = new DefaultHttpClient(httpParams);
return client;
}
/**
*
* 得到httpClient
*
* @return
*/
private static HttpClient getHttpClient() {
final HttpParams httpParams = new BasicHttpParams();
if (useProxy) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_CONNECTION);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_SOCKET);
HttpClientParams.setRedirecting(httpParams, true);
final String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.14) Gecko/20110218 Firefox/3.6.14";
HttpProtocolParams.setUserAgent(httpParams, userAgent);
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpClientParams.setCookiePolicy(httpParams, CookiePolicy.BROWSER_COMPATIBILITY);
HttpProtocolParams.setUseExpectContinue(httpParams, false);
HttpClient client = new DefaultHttpClient(httpParams);
return client;
}
/**
* 打印返回内容
* @param response
* @throws ParseException
* @throws IOException
*/
public static void showResponse(String str) throws Exception {
Gson gson = new Gson();
MapString, Object map = (MapString, Object) gson.fromJson(str, Object.class);
String value = (String) map.get("data");
//String decodeValue = Des3Request.decode(value);
//System.out.println(decodeValue);
//logger.debug(decodeValue);
}
/**
*
* 发起网络请求
*
* @param url
* URL
* @param requestData
* requestData
* @return INPUTSTREAM
* @throws AppException
*/
public static String doGet(String url) throws Exception {
String responseBody = null;
HttpGet httpGet = null;
HttpClient httpClient = null;
int statusCode = -1;
int time = 0;
do {
try {
httpGet = new HttpGet(url);
httpClient = getHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("HTTP" + " " + "HttpMethod failed: " + httpResponse.getStatusLine());
}
InputStream is = httpResponse.getEntity().getContent();
responseBody = getStreamAsString(is, HTTP.UTF_8);
break;
} catch (UnsupportedEncodingException e) {
time++;
if (time RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
} catch (ClientProtocolException e) {
time++;
if (time RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
} catch (IOException e) {
time++;
if (time RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生网络异常
e.printStackTrace();
} catch (Exception e) {
time++;
if (time RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生网络异常
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
httpClient = null;
}
} while (time RETRY_TIME);
return responseBody;
}
}
java后台怎么获取app传过来的数据
java后台写好接口,在APP端将要传递的数据封装好,然后调用后台的接口就是了
移动端调用的接口在java后台端用webservice的方式怎么写
把你要传递的数据转换成json字符串返回接口,然后手机端调用接口就可以获取到你要传递是值了
请问java web 后台如何接收app端的请求以及返回结果,跪求一个的helloword 版本
不用想那么复杂,既然你是学java后台的应该做过web项目吧,你只需要在后台写好方法,把请求地址给app那边让他访问就行,获取参数 就用request就可以,返回什么数据直接 return就可以啊。跳页面的操作应该是交给app吧。就是后台写个接口而已,给你例子 也就是接收到请求 返回一个字符串。 你是学过java之后 一直没做项目吗?
如何用java进行移动端后台开发
直接用java EE的结构编写即可,把传统要返回的html界面换成数据如json或者xml格式的数据就行。网络编程的本质都是客户端发出请求,服务端返回数据。传统的网站返回的是html数据,浏览器会自动对html数据进行解析并显示为我们平常看到的网页。而服务器要返回给移动端的话大都是json数据,json数据就需要android程序员或者ios程序员自己去解析了