當前位置:首頁 » 安卓系統 » android伺服器json

android伺服器json

發布時間: 2022-09-03 07:15:18

『壹』 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方式傳遞數據。伺服器端是怎麼接受和返回數據呢

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);

『叄』 webservice介面已經知道,android怎樣獲取伺服器中的json數據,怎樣在客戶端顯示出來

看你那邊什麼webservice介面,不一樣的介面,請求方式不一樣,但是多數是http請求介面,請求回來的數據,通過解析json格式,顯示到你要顯示的控制項就可以了。

『肆』 請問android怎樣通過json數據從伺服器獲取圖片

直接獲取是不行的,要有一個文件伺服器,對於文件伺服器會為每個圖片生成一個資源路徑,然後json數據中返回的就是這個資源路徑,最後用URL類就可以通過這個資源路徑把圖片download下來

『伍』 Android將伺服器傳來的json解析為數組變數,通過java文件simpleadapter輸出到頁面。

我做了一個代碼如下:
{

ListViewlistview;

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.listview);
Stringtemp="[{"aa":"1","bb":"2"},{"aa":"3","bb":"4"},{"aa":"5","bb":"6"}]";
List<Map<String,Object>>data=getList(temp);
SimpleAdapteradapter=newSimpleAdapter(this,data,R.layout.item,newString[]{"aa","bb"},newint[]{R.id.aa,R.id.bb});
listview.setAdapter(adapter);
}

publicMap<String,Object>getMap(StringjsonString){
JSONObjectjsonObject;
try{
jsonObject=newJSONObject(jsonString);
@SuppressWarnings("unchecked")
Iterator<String>keyIter=jsonObject.keys();
Stringkey;
Objectvalue;
Map<String,Object>valueMap=newHashMap<String,Object>();
while(keyIter.hasNext()){
key=(String)keyIter.next();
value=jsonObject.get(key);
valueMap.put(key,value);
}
returnvalueMap;
}catch(JSONExceptione){
e.printStackTrace();
}
returnnull;
}

publicList<Map<String,Object>>getList(StringjsonString){
List<Map<String,Object>>list=null;
try{
JSONArrayjsonArray=newJSONArray(jsonString);
JSONObjectjsonObject;
list=newArrayList<Map<String,Object>>();
for(inti=0;i<jsonArray.length();i++){
jsonObject=jsonArray.getJSONObject(i);
list.add(getMap(jsonObject.toString()));
}
}catch(Exceptione){
e.printStackTrace();
}
returnlist;
}
}

item的布局文件

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:id="@+id/aa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"/>

<TextView
android:id="@+id/bb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:textColor="#f00"
android:textSize="30sp"/>

</LinearLayout>


最後的界面效果:

『陸』 android應用怎麼樣在伺服器端解析從客戶端發送過來的json數據

首先是伺服器要取到數據,然後就是就在服務端解析json啊。。解析都是一樣的撒。。可以自己寫方法。也可以用別人的jar包。。

『柒』 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客戶端與伺服器發之間的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利用Json來進行網路數據傳輸

有點暈暈的,如果你是傳多個對象[{id:1,name:"tom",age:"20"},{id:2,name:"tom",age:"20"}]
怎麼搞成伺服器請求客戶端了??不是客戶端請求伺服器嗎?
一般JSON對應json都是通過id來對應的,我就是這樣對應的

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

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

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

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

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

熱點內容
存儲標准性 發布:2024-05-03 13:37:07 瀏覽:416
液鹼存儲 發布:2024-05-03 13:21:13 瀏覽:156
linux如何改配置文件 發布:2024-05-03 13:00:54 瀏覽:31
哪個安卓模擬器老爺機帶得動 發布:2024-05-03 13:00:49 瀏覽:576
編程與實戰 發布:2024-05-03 12:54:30 瀏覽:38
電腦開機有密碼rpc伺服器不可用 發布:2024-05-03 12:40:54 瀏覽:471
硬體的演算法 發布:2024-05-03 12:34:28 瀏覽:388
支付密碼為什麼就六位 發布:2024-05-03 12:29:17 瀏覽:920
腳本找書 發布:2024-05-03 12:02:17 瀏覽:493
境外伺服器租用怎麼辦 發布:2024-05-03 11:45:34 瀏覽:944