JsonURL
-https://dl.dropboxusercontent.com/s/rhk01nqlyj5gixl/jsonparsing.txt?token_hash=AAHpfauVxJaC9Rkx_5abNtJnPFG04os7TZky1AhOuC5jEw
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listViewID"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
</ListView>
</LinearLayout>
JSONParser.java
package com.example.testjson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONArray jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONArray getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dip"
android:maxLines="1"
android:text="Name"
android:textColor="@android:color/black"
android:textSize="14dp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/tvcity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvname"
android:layout_marginLeft="5dip"
android:layout_marginTop="3dip"
android:maxLines="1"
android:text="City"
android:textColor="@android:color/black"
android:textSize="14dp" >
</TextView>
<TextView
android:id="@+id/tvbdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvcity"
android:layout_marginLeft="5dip"
android:layout_marginTop="3dip"
android:maxLines="1"
android:text="Birthdate"
android:textColor="@android:color/black"
android:textSize="14dp" >
</TextView>
<TextView
android:id="@+id/tvgender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dip"
android:maxLines="1"
android:text="Gender"
android:textColor="@android:color/black"
android:textSize="14dp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/tvage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tvgender"
android:layout_marginRight="15dip"
android:layout_marginTop="3dip"
android:maxLines="1"
android:text="Age"
android:textColor="@android:color/black"
android:textSize="14dp" >
</TextView>
</RelativeLayout>
MainActivity.java
package com.example.testjson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.text.StaticLayout;
import android.view.Menu;
public class MainActivity extends Activity {
private static String url="https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create a JSON parser Instance ----- Used JSON parser from Android
JSONParser jParser=new JSONParser();
//Getting JSON string from URL ------ Used JSON Array from Android
JSONArray json=jParser.getJSONFromUrl(url);
try {
for(int i=0;i<json.length();i++)
{
JSONObject c=json.getJSONObject(i);// Used JSON Object from Android
//Storing each Json in a string variable
int AGE=c.getInt("age");
String NAME=c.getString("name");
String CITY=c.getString("city");
String GENDER=c.getString("Gender");
String BIRTHDATE=c.getString("birthdate");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
MainActivity.java
谢谢 ,
继续上课:
public class MainActivity extends Activity {
private static String url="https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create a JSON parser Instance ----- Used JSON parser from Android
JSONParser jParser=new JSONParser();
//Getting JSON string from URL ------ Used JSON Array from Android
JSONArray json=jParser.getJSONFromUrl(url);
List<WhateverObject> yourData = new ArrayList<WhateverObject>();
try {
for(int i=0;i<json.length();i++)
{
JSONObject c=json.getJSONObject(i);// Used JSON Object from Android
//Storing each Json in a string variable
int AGE=c.getInt("age");
String NAME=c.getString("name");
String CITY=c.getString("city");
String GENDER=c.getString("Gender");
String BIRTHDATE=c.getString("birthdate");
yourData.add(new WhateverObject(NAME, CITY, GENDER, BIRTHDATE));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListView yourListView = (ListView) findViewById(R.id.itemListView);
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, yourData);
yourListView.setAdapter(customAdapter);
}
}
调整器:
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView tt = null;
TextView tt1 = null;
TextView tt2 = null;
TextView tt3 = null;
TextView tt4 = null;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemlistrow, null);
tt = (TextView) v.findViewById(R.id.age);
tt1 = (TextView) v.findViewById(R.id.name);
tt2 = (TextView) v.findViewById(R.id.city);
tt3 = (TextView) v.findViewById(R.id.gender);
tt4 = (TextView) v.findViewById(R.id.birthdate);
}
Item p = items.get(position);
if (p != null) {
if (tt != null) {
tt.setText(""+p.getAge());
}
if (tt1 != null) {
tt1.setText(""+p.getName());
}
if (tt2 != null) {
tt2.setText(""+p.getCity());
}
if (tt3 != null) {
tt3.setText(""+p.getGender());
}
if (tt4 != null) {
tt4.setText(""+p.getBirthdate());
}
}
return v;
}
}
对象保存数据:
public class WhateverObject{
private int age;
private String name;
private String city;
private String gender;
private String birthdate;
public WhateverObject(int age, String name, String city, String gender, String birthdate){
this.age = age;
this.name = name;
this.city = city;
this.gender = gender;
this.birthdate = birthdate;
}
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
public String getCity(){
return this.city;
}
public String getGender(){
return this.gender;
}
public String getBirthdate(){
return this.birthdate;
}
}
列表项的xml(保存在itemlistrow
名称下):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_width="fill_parent">
<TableRow android:layout_width="fill_parent"
android:id="@+id/TableRow01"
android:layout_height="wrap_content">
<TextView
android:id="@+id/age"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="age" android:textStyle="bold"
android:gravity="left"
android:layout_weight="1"
android:typeface="monospace"
android:height="40sp"/>
</TableRow>
<TableRow android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="name"
android:layout_weight="1"
android:height="20sp"/>
</TableRow>
<TableRow android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="city"
android:layout_weight="1"
android:height="20sp"/>
</TableRow>
<TableRow android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/gender"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="city"
android:layout_weight="1"
android:height="20sp"/>
</TableRow>
<TableRow android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/birthdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="city"
android:layout_weight="1"
android:height="20sp"/>
</TableRow>
</TableLayout>
比较简单的例子:
public class MainActivity extends Activity {
private static String url="https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create a JSON parser Instance ----- Used JSON parser from Android
JSONParser jParser=new JSONParser();
//Getting JSON string from URL ------ Used JSON Array from Android
JSONArray json=jParser.getJSONFromUrl(url);
List<Map<String, String>> personList = new ArrayList<Map<String,String>>();
try {
for(int i=0;i<json.length();i++)
{
JSONObject c=json.getJSONObject(i);// Used JSON Object from Android
//Storing each Json in a string variable
int AGE=c.getInt("age");
String NAME=c.getString("name");
String CITY=c.getString("city");
String GENDER=c.getString("Gender");
String BIRTHDATE=c.getString("birthdate");
personList.add(createPerson(AGE, NAME, CITY, GENDER, BIRTHDATE));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListView yourListView = (ListView) findViewById(R.id.itemListView);
simpleAdpt = new SimpleAdapter(this, personList, android.R.layout.simple_list_item_1, new String[] {"person"}, new int[] {android.R.id.text1});
yourListView.setAdapter(simpleAdpt);
}
private HashMap<String, String> createPerson(int age, String name, String city, String gender, String birthdate) {
HashMap<String, String> person = new HashMap<String, String>();
person.put("person", name+" | "+age + " | "+city + " | "+gender + " | "+birthdate);
return person;
}
}
感谢http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-
example.html
我有两个视图,适合在屏幕的顶部和底部,我有一个回收器视图,将坐在他们之间,但有一个视图,很多与回收器视图垂直包装。类似这样的东西 我必须使用约束布局来实现这一点。我尝试过使用打包链来实现这一点,但这将视图放在屏幕的中心,而不是与顶视图对齐。
问题内容: 如何使用html链接打开带有预填充内容的短信应用? 我阅读的所有内容似乎都表明sms:18005555555?body = bodyTextHere 应该可以,但是在iPhone上不起作用。如果我将?body = bodyTextHere取出,然后仅使用sms:phonenumber,它将起作用。 我见过几个通过野生动物园链接执行QR码的实例。他们如何能够预先填充正文? 问题答案: 事
问题内容: 我正在使用Spring-data- rest在某些JPA实体上提供读取的API。对于写操作,我需要发出Command对象,而不是直接写到DB,因此我添加了一个使用各种命令处理方法的自定义控制器: 我希望输出像spring-data-rest控制器的任何其他响应一样,得到充实,特别是我希望它为自身及其关系添加HATEOAS链接。 问题答案: 最近,Oliver Gierke亲自回答了这个
问题内容: 我有一个JSON请求,该请求从youtube返回一个响应,其中包含对特定视频的评论。我目前有3种文本视图:一种用于名称/上载器,一种用于内容,一种用于发布日期- 然后用我的JSON响应中的数据填充。 我的问题是-仅出现第一个评论,发布日期和上传者。 我相信我将需要用列表视图替换我的textviews并将其解析为3个字段-我只是不知道如何。 爪哇 公共类Player扩展了YouTubeB
问题内容: 我有一个数据表,每个单元格都是一个链接。我想允许用户单击表格单元格中的任何位置,并让他们点击链接。有时表单元格不止一行,但并非总是如此。我使用td{display:block}来获得覆盖大部分单元格的链接。当一行中有一个单元格为两行,而其他单元格只有一行时,一个衬板不会填满表格行的整个垂直空间。这是示例HTML,您可以在上看到它的运行方式: 问题答案: 您需要对CSS进行一些小的更改。
我需要使用AES-256-CBC和PKCS7填充加密用户和密码,以连接到它不是我的.NET服务器应用程序。 首先,我使用Diffie-Hellman算法获得了公共服务器密钥,遵循本网站上的说明: https://doc.developer.milestonesys.com/mipsdkmobile/index.html?base=content_0.html 从服务器获取公钥的代码是: 一旦我拿到