日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

app开发历程————Android程序解析服务器端的JSON格式数据,显示在界面上

發布時間:2023/12/13 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 app开发历程————Android程序解析服务器端的JSON格式数据,显示在界面上 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上一篇文章寫的是服務器端利用Servlet 返回JSON字符串,本文主要是利用android客戶端訪問服務器端鏈接,解析JSON格式數據,放到相應的位置上。

首先,android程序的布局文件main.xml

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" 10 android:orientation="vertical"> 11 12 <TextView 13 android:id="@+id/textView" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 /> 17 18 </LinearLayout>

MainActivity.java

1 package com.practice; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 6 import org.apache.http.HttpEntity; 7 import org.apache.http.HttpResponse; 8 import org.apache.http.client.HttpClient; 9 import org.apache.http.client.methods.HttpGet; 10 import org.apache.http.impl.client.DefaultHttpClient; 11 import org.apache.http.params.HttpConnectionParams; 12 import org.apache.http.params.HttpParams; 13 import org.json.JSONArray; 14 import org.json.JSONObject; 15 16 import android.os.Bundle; 17 import android.app.Activity; 18 import android.util.Log; 19 import android.view.Menu; 20 import android.widget.TextView; 21 22 public class MainActivity extends Activity { 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.main); 28 Log.v("111111111", "1111111111111111"); 29 30 31 32 try{ 33 StringBuffer sb=new StringBuffer(); 34 String url="http://192.168.0.251:8080/ServletTest/test"; 35 String body=getContent(url); 36 Log.v("222222", body); 37 JSONArray array=new JSONArray(body); 38 //JSONObject ty=new JSONObject(body); 39 for(int i=0;i<array.length();i++){ 40 41 JSONObject obj=array.getJSONObject(i); 42 sb.append("id:").append(obj.getInt("id")).append("\t"); 43 sb.append("name:").append(obj.getString("name")).append("\t"); 44 sb.append("gender:").append(obj.getString("gender")).append("\t\n"); 45 sb.append("email:").append(obj.getString("email")).append("\t"); 46 sb.append("----------------------\n"); 47 48 } 49 Log.v("333333333333", sb.toString()); 50 TextView textView =(TextView) findViewById(R.id.textView); 51 textView.setText(sb.toString()); 52 53 54 55 } 56 catch(Exception e) 57 { 58 59 Log.v("11111111111", e.toString()); 60 Log.v("11111111111", "msg"); 61 } 62 63 } 64 65 66 private String getContent(String url) throws Exception{ 67 68 69 StringBuilder sb=new StringBuilder(); 70 HttpClient client=new DefaultHttpClient(); 71 HttpParams httpParams=client.getParams(); 72 73 HttpConnectionParams.setConnectionTimeout(httpParams, 3000); 74 HttpConnectionParams.setSoTimeout(httpParams, 5000); 75 76 HttpResponse response =client.execute(new HttpGet(url)); 77 HttpEntity entity =response.getEntity(); 78 79 if(entity!=null){ 80 BufferedReader reader=new BufferedReader(new InputStreamReader(entity.getContent())); 81 String line =null; 82 while((line=reader.readLine())!= null){ 83 sb.append(line+"\n"); 84 } 85 reader.close(); 86 87 } 88 return sb.toString(); 89 90 91 } 92 93 94 @Override 95 public boolean onCreateOptionsMenu(Menu menu) { 96 // Inflate the menu; this adds items to the action bar if it is present. 97 getMenuInflater().inflate(R.menu.main, menu); 98 return true; 99 } 100 101 }

在AndroidManifest.xml,設定第一啟動界面,開始運行

?運行如下:

成功啦!一路上,無論多么坎坷,路還是要走,日子還需要照過!

轉載于:https://www.cnblogs.com/woxiangxintj/p/3963715.html

總結

以上是生活随笔為你收集整理的app开发历程————Android程序解析服务器端的JSON格式数据,显示在界面上的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。