Ⅰ Android请求php服务器的JSON问题
因为PHP会默认添加一个bom头信息 有几个字节 你得去掉 一般是四个字节
package cn.image.sky.util;
import java.io.*;
/**
* Generic unicode textreader, which will use BOM mark to identify the encoding
* to be used. If BOM is not found then use a given default or system encoding.
*/
public class UnicodeReader extends Reader {
PushbackInputStream internalIn;
InputStreamReader internalIn2 = null;
String defaultEnc;
private static final int BOM_SIZE = 4;
/**
*
* @param in
* inputstream to be read
* @param defaultEnc
* default encoding if stream does not have BOM marker. Give NULL
* to use system-level default.
*/
UnicodeReader(InputStream in, String defaultEnc) {
internalIn = new PushbackInputStream(in, BOM_SIZE);
this.defaultEnc = defaultEnc;
}
public String getDefaultEncoding() {
return defaultEnc;
}
/**
* Get stream encoding or NULL if stream is uninitialized. Call init() or
* read() method to initialize it.
*/
public String getEncoding() {
if (internalIn2 == null)
return null;
return internalIn2.getEncoding();
}
/**
* Read-ahead four bytes and check for BOM marks. Extra bytes are unread
* back to the stream, only BOM bytes are skipped.
*/
protected void init() throws IOException {
if (internalIn2 != null)
return;
String encoding;
byte bom[] = new byte[BOM_SIZE];
int n, unread;
n = internalIn.read(bom, 0, bom.length);
if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00)
&& (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) {
encoding = "UTF-32BE";
unread = n - 4;
} else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)
&& (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) {
encoding = "UTF-32LE";
unread = n - 4;
} else if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB)
&& (bom[2] == (byte) 0xBF)) {
encoding = "UTF-8";
unread = n - 3;
} else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
encoding = "UTF-16BE";
unread = n - 2;
} else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
encoding = "UTF-16LE";
unread = n - 2;
} else {
// Unicode BOM mark not found, unread all bytes
encoding = defaultEnc;
unread = n;
}
// System.out.println("read=" + n + ", unread=" + unread);
if (unread > 0)
internalIn.unread(bom, (n - unread), unread);
// Use given encoding
if (encoding == null) {
internalIn2 = new InputStreamReader(internalIn);
} else {
internalIn2 = new InputStreamReader(internalIn, encoding);
}
}
public void close() throws IOException {
init();
internalIn2.close();
}
public int read(char[] cbuf, int off, int len) throws IOException {
init();
return internalIn2.read(cbuf, off, len);
}
}
这个类 你看看 可以解决你的问题
Ⅱ Android 解析json问题
///http地址
StringhttpUrl=ip+":"+端口号+"/loginbyandroid/validate.do";
//HttpPost连接对象
HttpPosthttpRequest=newHttpPost(httpUrl);
//使用NameValuePair来保存要传递的Post参数
List<NameValuePair>params=newArrayList<NameValuePair>();
//添加要传递的参数
params.add(newBasicNameValuePair("loginId","value"));
params.add(newBasicNameValuePair("password","value"));
//设置字符集
HttpEntityhttpentity;
try{
httpentity=newUrlEncodedFormEntity(params,"utf-8");
//请求httpRequest
httpRequest.setEntity(httpentity);
//取得默认的HttpClient
HttpClienthttpclient=newDefaultHttpClient();
//取得HttpResponse
HttpResponsehttpResponse;
httpResponse=httpclient.execute(httpRequest);
//HttpStatus.SC_OK表示连接成功
if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
//取得返回的字符串
StringstrResult=EntityUtils.toString(httpResponse
.getEntity());
JSONArrayjsonArray=newJSONArray(strResult);
for(inti=0;i<jsonArray.length();i++){
JSONObjectjsonObject=(JSONObject)jsonArray.opt(i);
Stringsuccess=jsonObject.getString("success");
StringJSESSIONID=jsonObject.getString("JSESSIONID");
StringloginName=jsonObject.getString("loginName");
Stringorgname=jsonObject.getString("orgname");
System.out.println("success="+success
+"JSESSIONID="+JSESSIONID+"loginName="
+loginName+"orgname="+orgname);
}
}else{
System.out.println("请求错误!");
}
}catch(ClientProtocolExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
Ⅲ android 中如何在 手机端发送json请求,服务器得到json类型的响应结果
android中要求向服务器发送get,然猴得到响应数据
Ⅳ android post请求json参数list认证怎样实现
如果采用post请求,与后台传送参数采用json格式,那么可以采用如下的形式包装参数:
JSONObject params = new JSONObject();
params.put("signature",signature);
params.put("timestamp",timestamp);
params.put("nouce",nouce);
params.put("parnter",parnter);
params.put("access_token",access_token);
包装之后可以采用一个访问网络的工具类HttpClient访问后台接口就可以了
我不知道你说的是不是这个意思,希望帮到你
Ⅳ java怎么接收android请求过来的json数据
java接收android请求json数据的方法:
如果发送的没有参数名称你可以直接得到请求体,如
InputStreaminputStream=urlConnection.getInputStream();
Stringencoding=urlConnection.getContentEncoding();
Stringbody=IOUtils.toString(inputStream,encoding);
System.out.println(body);
如果body就是那个json内容使用fastjson进行解析就可以了
JSONObjectmap=JSON.parseObject(body);
System.out.println(map.getString("mobileNo"));//还是System.out.println(map.get("mobileNo"));?具体看一下接口文档
或者
Mapmap=JSON.parseObject(body,Map.class);
System.out.println(map.get("mobileNo"));
Ⅵ android怎么使用okhttputils发送json请求数据
服务端是用servlet写的吧
直接调用response的out输出即可
response.getWriter().print("20"); 这样安卓得到的返回值就是20