編輯:關於Android編程
package cc.test; import android.app.Activity; import android.os.Bundle; /** * Demo描述: * 利用Gson實現對象和Json數據的相互轉換 * * Demo描述: * 通過一個網絡請求,獲取JSON數據 * * 注意: * 1 網絡請求的參數是JSON格式的數據 * 2 請求結果返回的亦是JSON格式的數據 * */ public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ new Thread(){ public void run(){ GetJsonDataByPost httpJsonPost=new GetJsonDataByPost(); String[] pathArray=httpJsonPost.getPathArray("dev0003"); for(int i=0;i
GetJsonDataByPost如下:
package cc.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpConnectionParams; import org.apache.http.protocol.HTTP; public class GetJsonDataByPost { static private String Service_URL = "Your url"; static private int TIMEOUT = 120 * 1000; String mMethodName; public String[] getPathArray(String devnum) { try { mMethodName = "GetPicByUser"; String[] pathArray = null; //將調用API的參數封裝成JSON格式 String jsonParams = JsonUtils.getJsonRequestParams(devnum); //返回的JSON數據 String jsonResult = getJsonDataByPost(jsonParams); //從返回的JSON數據獲取其包含的一個數組 pathArray = JsonUtils.getJsonRequestResponse(jsonResult); return pathArray; } catch (Exception e) { e.printStackTrace(); } return null; } public String getJsonDataByPost(String json) { String result = null; try { StringEntity entity = new StringEntity(json, HTTP.UTF_8); entity.setContentType("application/json"); DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, TIMEOUT); client.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, TIMEOUT); if(mMethodName == null){ return null; } HttpPost httpPost = new HttpPost(Service_URL + mMethodName); httpPost.setEntity(entity); HttpResponse response = client.execute(httpPost); InputStream inputStream = response.getEntity().getContent(); StringBuffer buffer = new StringBuffer(); InputStreamReader inputReader = new InputStreamReader(inputStream); BufferedReader bufferReader = new BufferedReader(inputReader); String str = new String(""); while ((str = bufferReader.readLine()) != null) { buffer.append(str); } bufferReader.close(); result = buffer.toString(); System.out.println("---> API的請求結果 result="+result); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } }
JsonUtils如下:package cc.test; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; public class JsonUtils { //該對象用於封裝請求API的參數. //請求時會將該對象轉換為JSON格式的數據 static class JsonRequestParams { String devsn; int uploadid; float healthScore; } //該對象用於封裝請求API返回後的結果. //即會將JSON格式的數據結果封裝成該對象 static class JsonRequestResult { String resultcode; int uploadid; String[] pics; float beat; String crtime; } //將請求的參數封裝成JSON的格式 public static String getJsonRequestParams(String devnum) { try { JsonRequestParams jsonRequestParams = new JsonRequestParams(); jsonRequestParams.devsn = devnum; jsonRequestParams.uploadid = 0; jsonRequestParams.healthScore = 0.0f; Gson gson = new Gson(); //將對象轉換為JSON數據 String jsonRequestParamsString = gson.toJson(jsonRequestParams); System.out.println("---> 封裝後的API請求參數 jsonRequestParamsString="+jsonRequestParamsString); return jsonRequestParamsString; } catch (Exception e) { e.printStackTrace(); } return null; } public static String[] getJsonRequestResponse(String ret){ try { Gson gson = new Gson(); //將返回的JSON數據轉換為對象JsonRequestResult JsonRequestResult mJsonGetPicResponse = gson.fromJson(ret, JsonRequestResult.class); if(mJsonGetPicResponse.resultcode.contains("ok")){ //從對象中獲取除pics外的各個字段且輸出顯示 System.out.println("---> mJsonGetPicResponse.resultcode="+mJsonGetPicResponse.resultcode); System.out.println("---> mJsonGetPicResponse.beat="+mJsonGetPicResponse.beat); System.out.println("---> mJsonGetPicResponse.uploadid="+mJsonGetPicResponse.uploadid); System.out.println("---> mJsonGetPicResponse.crtime="+mJsonGetPicResponse.crtime); //從對象中獲取pics字段,且返回 return mJsonGetPicResponse.pics; } } catch (JsonSyntaxException e) { e.printStackTrace(); } return null; } }
main.xml如下:
首先,我們要把圖片存入到數據庫中,首先要創建一個數據庫, 如下所示:復制代碼 代碼如下:package com.android.test;import java.io.B
當用戶輸入一定的字符之後,自動完成文本框能夠顯示一個下拉菜單,供用戶從中選擇,當用戶選擇某個菜單項之後,AutoCompleteTextView可以按照用戶的選擇自動填寫
最近瞄到一個小項目….Yalantis/Context-Menu.Android打開這個項目,我只想看看那個動態展開的menu是怎麼實現的.忽
相對布局要比前面講的線性布局和表格布局要靈活一些,所以平常用得也是比較多的。相對布局控件的位置是與其周圍控件的位置相關的,從名字可以看出來,這些位置都是相對的,確定出了其