當前位置:首頁 » 安卓系統 » androidjson伺服器交互

androidjson伺服器交互

發布時間: 2022-11-16 13:52:39

Ⅰ 大牛們,安卓和伺服器使用json交換數據,該怎麼加密

JSONObject json = new JSONObject(); json.put(key,value); . String s = json.toString(); HttpClient client =new DefaultHttpClient(); HttpPost post = new HttpPost(url); HttpEntity entity = new StringEntity(s); post.setEntity(en

Ⅱ Android向伺服器發送json數據,伺服器怎麼把post過去的數據取出來

request取,如果是用了框架,那就直接把注入形參的內容取出來就可以了。

Ⅲ 在android網路編程里,客戶端與伺服器端採用json方式傳遞數據。伺服器端是怎麼接受和返回數據呢

int formDataLength = request.getContentLength();
// 取得ServletInputStream輸入流對象
DataInputStream dataStream = new DataInputStream(
request.getInputStream());
byte body[] = new byte[formDataLength];
int totalBytes = 0;
while (totalBytes < formDataLength) {
int bytes = dataStream.read(body, totalBytes, formDataLength);
totalBytes += bytes;
}
String json = new String(body, "ISO-8859-1");
System.out.println(json);

Ⅳ android客戶端與伺服器發之間的json數據解析

JSONArray jsonArray = new JSONArray(string); //string為返回的字元串
//數據直接為一個數組形式,所以可以直接 用android提供的框架JSONArray讀取JSON數據,轉換成Array
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i);
//每條記錄又由幾個Object對象組成
iitem.getInt("index");
// 獲取對象對應的值
item.getString("address");
}

Ⅳ android用volley怎麼給伺服器發送json

1.下載官網的android SDK(本人用的是eclipse)

2.新建一個android項目:

File->new->andriod Application project

7、下面就是具體的使用post和get請求的代碼:

A:發送get請求如下:

package com.example.xiaoyuantong;

import java.util.HashMap;

import java.util.Iterator;

import org.json.JSONException;

import org.json.JSONObject;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.widget.TextView;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.Volley;

/**

* Demo

*/

public class MainActivity extends Activity {

private RequestQueue requestQueue ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

private void init() {

TextView textView = (TextView)findViewById(R.id.textView);

requestQueue = Volley.newRequestQueue(this);

getJson();

textView.setText("hello");

}

private void getJson(){

String url = "http://192.168.20.1:8080/xiaoyuantong/userAction!register.action?pwd='測試'";

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(

Request.Method.GET, url, null,

new Response.Listener<JSONObject>() {

@Override

public void onResponse(JSONObject response) {

//這里可以列印出接受到返回的json

Log.e("bbb", response.toString());

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError arg0) {

// System.out.println("sorry,Error");

Log.e("aaa", arg0.toString());

}

});

requestQueue.add(jsonObjectRequest);

}

}

B:發送post請求如下:

package com.example.xiaoyuantong;

import java.util.HashMap;

import java.util.Map;

import org.json.JSONException;

import org.json.JSONObject;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.Volley;

import android.os.Bundle;

import android.app.Activity;

import android.util.Log;

import android.view.Menu;

import android.widget.TextView;

public class PostActivity extends Activity {

private RequestQueue requestQueue ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_post);

init();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.post, menu);

return true;

}

private void init() {

TextView textView = (TextView)findViewById(R.id.postView);

requestQueue = Volley.newRequestQueue(this);

getJson();

textView.setText("hellopost");

}

private void getJson(){

String url = "http://192.168.20.1:8080/xiaoyuantong/userAction!reg.action";

JsonObjectRequest jsonObjectRequest ;

JSONObject jsonObject=new JSONObject() ;

try {

jsonObject.put("name", "張三");

jsonObject.put("sex", "女");

} catch (JSONException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

//列印前台向後台要提交的post數據

Log.e("post",jsonObject.toString());

//發送post請求

try{

jsonObjectRequest = new JsonObjectRequest(

Request.Method.POST, url, jsonObject,

new Response.Listener<JSONObject>() {

@Override

public void onResponse(JSONObject response) {

//列印請求後獲取的json數據

Log.e("bbb", response.toString());

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError arg0) {

// System.out.println("sorry,Error");

Log.e("aaa", arg0.toString());

}

});

requestQueue.add(jsonObjectRequest);

} catch (Exception e) {

e.printStackTrace();

System.out.println(e + "");

}

requestQueue.start();

}

}

8、在android的logcat裡面能查看到列印的請求

(紅色的顯示的是我在後台請求到數據)

有時候logcat顯示不出數據,可能是消息被過濾了,可以在左邊點擊「減號」刪除過濾

在server端,也就是在myeclipse的建立的另一個後台工程裡面能獲取到請求:


9、後續會補充json數據的解析部分,以及過度到移動雲的部分,上面只是c/s模式下的一個簡單的基於http的請求應答例子。

Ⅵ android寫json格式介面,怎麼保證安全性

一般客戶端與伺服器的交互根據對安全的要求不同,遵循不同的加密級別 1、快速且不安全 http明文交互 2、加密驗證防惡意篡改 在http響應里加入md5驗證 3、防中間人 在http響應里加入伺服器私鑰簽名 4、安全交互 https 每種交互都有自己的應用場景,歡迎補充

Ⅶ android 在伺服器端生成json格式數據,在客戶端怎麼解析

1、生成JSON格式數據,有對應的後台類處理,如果你是做Android開發,後台提供獲取數據的介面


2、客戶端解決:

JSONArrayjsonArr=newJSONArray(json);
for(inti=0;i<jsonArr.length();i++){
JSONObjectjsonObj=jsonArr.getJSONObject(i);
booleanisChild=jsonObj.has("childrenNodes");
AreaBeanbean=newAreaBean(jsonObj.getString("id"),
jsonObj.getString("parentId"),
jsonObj.getString("name"));
mList.add(bean);
if(isChild){
mchildNodesList.add(jsonObj.getString("childrenNodes"));
}else{
mchildNodesList.add(null);
}
}

Ⅷ 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格式,請問用webservice好呢還是http好呢

http比較好,一般公司都用的是http。對應json來說已盡夠了

Ⅹ 伺服器端怎麼接收Android客戶端傳過來的Json數據

android如果是通過http post發送數據的話,可以採用以下方式接收數據:

  1. 通過request.getParameter(paraName); 獲取參數。

  2. request對象就是表示請求對象,getParameter就是獲取參數,傳遞的參數就是參數名。

  3. 例如請求 localhost:8080/web?data=abcd 則伺服器取值,request.getParameter("data"); 。

熱點內容
oraclesql動態 發布:2025-03-04 12:44:19 瀏覽:229
MPLAB能否編譯pic32 發布:2025-03-04 12:42:50 瀏覽:286
如何分辨一輛車是什麼配置 發布:2025-03-04 12:41:10 瀏覽:346
配置很低的電腦玩csgo怎麼調 發布:2025-03-04 12:40:29 瀏覽:22
視頻解析網站源碼 發布:2025-03-04 12:40:25 瀏覽:315
哪個軟體可以直接重啟安卓手機 發布:2025-03-04 12:22:42 瀏覽:608
c語言scanf的意思 發布:2025-03-04 12:08:31 瀏覽:467
兩端存儲器 發布:2025-03-04 12:07:49 瀏覽:85
安卓手機連不上無線網什麼原因 發布:2025-03-04 12:03:29 瀏覽:91
串口緩存區 發布:2025-03-04 11:59:12 瀏覽:579