androidjson服务器交互
Ⅰ 大牛们,安卓和服务器使用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发送数据的话,可以采用以下方式接收数据:
通过request.getParameter(paraName); 获取参数。
request对象就是表示请求对象,getParameter就是获取参数,传递的参数就是参数名。
例如请求 localhost:8080/web?data=abcd 则服务器取值,request.getParameter("data"); 。