导航:首页 > 操作系统 > android分享url

android分享url

发布时间:2025-01-01 04:53:40

‘壹’ 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 发送url带中文出现乱码怎么解决

服务器用的是utf-8处理的吧,如果是,客户端使用mDetailText.getText().toString() .getBytes("UTF-8")获得字节数组,然后用stream发送。android的乱码和PC机上的乱码的原因是相同的。可采用同样的解决方案。

阅读全文

与android分享url相关的资料

热点内容
单片机易混淆概念 浏览:591
stata面板固定效应回归命令 浏览:862
lol手游连接不上服务器怎么回事 浏览:17
算法工程师求职感悟 浏览:486
六一快乐解压图 浏览:530
effectivecpdf英文 浏览:276
查找ip地址命令 浏览:878
中国注册会计师协会算法人吗 浏览:469
什么叫服务器内部错误4o4 浏览:495
单片机原理复习 浏览:435
打不开头条任务服务器怎么办 浏览:646
战雷录像在哪个文件夹 浏览:465
加密验证的门禁卡 浏览:741
java输出心 浏览:532
程序员的身体素质调查 浏览:383
python做一个登录系统怎么改界面 浏览:884
杨颖缓解压力的方法 浏览:144
约苗app在哪里改地址 浏览:819
tuxedo的加密协议 浏览:729
javapdftojpg 浏览:715