android用volley怎么给服务器发送json

android用volley怎么给服务器发送json,第1张

1.  下载官网的android SDK(本人用的是eclipse)

2.  新建一个android项目:

File->new->andriod Application project

3、点击next,建立第一个mainActivity

4、启动服务器端,本人采用的是ssh搭建的server端,能接受前台的post或get请求,然后返还json数据(本例子,主要演示如何在android发送post和get请求)

6、下面有很关键的几步的,与在本地编写web有所不同:

A:android端的url要写上服务器端的ip:

如下:

String url = "http://192.168.20.1:8080/xiaoyuantong/userAction!register.action?pwd='测试'"

(后面我会给出完整的代码)

B、要在AndroidManifest.xml文件中添加:

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 menuthis 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的请求应答例子。

发音:英 [ˈvɒli]   美 [ˈvɑːli] 

volley,英语单词,主要用作名词、及物动词、不及物动词,作名词时译为“齐射;齐射出的子d;凌空状态”,作及物动词时译为“截击;齐发;连声发出”,作不及物动词时译为“截击;齐鸣;进行群射”。

单词用法:

1、V-T/V-I In sports, if someone volleys the ball or if they volley, they hit the ball before it touches the ground. 截击空中球

2、N-COUNTVolley is also a noun. 截击空中球

3、N-COUNT A volley of gunfire is a lot of bullets that travel through the air at the same time. (炮火的) 齐发

短语搭配:

1、stop volley 截停球 停空中球 贴网下落的扣球

2、volley hunks 排球对战

3、overhead volley 倒钩球


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/7841939.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存