導航:首頁 > 文件教程 > android更新伺服器端的json文件

android更新伺服器端的json文件

發布時間:2024-12-19 00:29:40

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

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

2.新建一個項目:

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的請求應答例子。

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

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

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

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

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

C. android 開發中怎樣從手機客戶端操作 從伺服器資料庫中獲取信息 以及更新資料庫數據

用xml或json,把數據傳到伺服器哪邊去,然後伺服器再把數據存到資料庫。

D. android如何獲取伺服器端文件列表及相關信息

要弄的話你先要搞清幾個問題,
1、你與服務端的通信協議。如果一般服務端已經開發好了,那麼會有一套通信協議,通常與手機的交互都採用JSON格式發送,減少流量。你可以網路下JSON的相關知識。很簡單的一種格式。如果不是JSON的話一般會是XML,不過很少見。
2、數據量。Android market應用列表見過吧?很多情況下回做成懶載入,而不是刷新所有數據。這樣的話你就要根據數據量考慮你的代碼實現了,合理的使用SoftReference,優化ListView,用sqllite資料庫緩存數據等等,具體機制你需要自己設計一下了,如果你是個PM的話。如果不是PM推薦你找個有經驗的人設計下,否則很容易出現OOM異常

對於數據的處理方面,就像第一條說的,無論是JSON還是XML格式,android都有工具類,用法我就不貼了,自己搜一下,很多的

閱讀全文

與android更新伺服器端的json文件相關的資料

熱點內容
maya粒子表達式教程 瀏覽:84
抖音小視頻如何掛app 瀏覽:283
cad怎麼設置替補文件 瀏覽:790
win10啟動文件是空的 瀏覽:397
jk網站有哪些 瀏覽:134
學編程和3d哪個更好 瀏覽:932
win10移動硬碟文件無法打開 瀏覽:385
文件名是亂碼還刪不掉 瀏覽:643
蘋果鍵盤怎麼打開任務管理器 瀏覽:437
手機桌面文件名字大全 瀏覽:334
tplink默認無線密碼是多少 瀏覽:33
ipaddgm文件 瀏覽:99
lua語言編程用哪個平台 瀏覽:272
政采雲如何導出pdf投標文件 瀏覽:529
php獲取postjson數據 瀏覽:551
javatimetask 瀏覽:16
編程的話要什麼證件 瀏覽:94
錢脈通微信多開 瀏覽:878
中學生學編程哪個培訓機構好 瀏覽:852
榮耀路由TV設置文件共享錯誤 瀏覽:525

友情鏈接