當前位置:首頁 » 安卓系統 » androidjson請求

androidjson請求

發布時間: 2022-09-28 20:04:58

Ⅰ 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

熱點內容
腳本手機電腦通用嗎 發布:2022-11-28 01:52:41 瀏覽:955
存儲卡處理 發布:2022-11-28 01:48:10 瀏覽:285
編譯器怎麼轉移到其他盤找 發布:2022-11-28 01:41:34 瀏覽:686
修空調壓縮機壞了 發布:2022-11-28 01:38:33 瀏覽:674
寶馬汽車有哪些配置 發布:2022-11-28 01:38:29 瀏覽:815
mysql服務啟動linux 發布:2022-11-28 01:35:43 瀏覽:794
ftp密碼怎麼修改密碼 發布:2022-11-28 01:35:28 瀏覽:964
幼兒園家訪問卷 發布:2022-11-28 01:34:42 瀏覽:793
我的世界him伺服器 發布:2022-11-28 01:33:43 瀏覽:423
安卓幸運破解器怎麼用 發布:2022-11-28 01:28:41 瀏覽:248