练手小项目(2)生活小助手周公解梦
第一篇
第二篇
我在想就是第三个药品查询要不要写出来,因为布局还在讨论用什么展示,因为药品有很多展示,我也不知道用什么展示。 这是一个很纠结的事情 我就先写第四个吧 周公解梦

其中代码有点错误我想用for循环进行判断返回数据有几个 但是总是失败,如果有看本篇贴子,解决了,给我留个言,在这篇帖子我只显示一个结果
布局跟简单的说 一个Edittext 获取数据,然后button进行数据提取发送到服务器 返回的数据 进行json解析展示在下面的textview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center_horizontal"
android:text="输入你想查询的梦境" />
<EditText
android:id="@+id/dream_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:hint="美女,黄金"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/dream_serhch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dream_num"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:onClick="btn"
android:text="查询" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_marginTop="25dp"
android:layout_height="wrap_content"
android:layout_below="@id/dream_serhch"
android:orientation="vertical" >
<TextView
android:id="@+id/title_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title_one" />
<TextView
android:id="@+id/content_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容一" />
<TextView
android:id="@+id/title_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="title_two" />
<TextView
android:id="@+id/content_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容二" />
<TextView
android:id="@+id/title_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="title_two" />
<TextView
android:id="@+id/content_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容二" />
</LinearLayout>
</RelativeLayout>
好了 看 java代码 都写了注释的
package com.example.helper;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.example.helper.bean.DreamResult;
import com.example.helper.utils.HttpUtils;
import com.example.helper.utils.HttpUtils.OnNetWorkResponse;
import com.example.helper.utils.UrlApi;
public class DreamSearch extends Activity {
//展示内容的textview
private TextView title_one, content_one, title_two, content_two,
title_three, content_three;
//拼接的url
private String url;
//输入信息的Edittext
private EditText dream_num;
//url编码后的url
private String encode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dream_search);
// 1.实例化组件
initView();
}
private void initView() {
dream_num = (EditText) findViewById(R.id.dream_num);
title_one = (TextView) findViewById(R.id.title_one);
content_one = (TextView) findViewById(R.id.content_one);
title_two = (TextView) findViewById(R.id.title_two);
content_two = (TextView) findViewById(R.id.content_two);
title_three = (TextView) findViewById(R.id.title_three);
content_three = (TextView) findViewById(R.id.content_three);
}
public void btn(View view) {
//获取用户输入的edittext
String str = dream_num.getText().toString().trim();
try {
//使用urlEncode 进行转换 成url编码格式 发送
encode = URLEncoder.encode(str, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//拼接字符串 成url
url = UrlApi.Four + encode;
HttpUtils.RequestNetWork(url, new OnNetWorkResponse() {
@Override
public void ok(String response) {
try {
/*json格式
* {
"error_code": 0,
"reason": "successed",
"result": [
{
"des": "梦见穿连身的窄裙套装,你的性格可能是比较缺乏安全感,所以你潜意识中的美人模样,就是一副很能干、成熟稳重的样子。这可能和你从小对母亲印象的反射有关,或许你的母亲很能干,或许你的母爱不够,曾经在你的童年经验中,留下这种缺陷和不足,因为不足,所以会有需求的渴望,你的潜意识中才会梦到这种女人来给你安全感。",
"id": "2b1a12a98f719278090c9eb274f98330",
"title": "美女的穿着"
},
{
"des": "梦见与美貌女子交往,为破财之兆。",
"id": "428d02b991f501ca46be297eca767485",
"title": "美女"
},
{
"des": "梦见和美女做爱,其实这是一种正常的生理反应,当然你也不泛曾对女性的幻想,这样就自然而然地做出这种和美女做爱的梦境,以达到泄精的目的。",
"id": "e9232f4837c91d1c50cfc6972c2876f1",
"title": "和美女做爱"
},
{
"des": "梦见与美貌女子交往,为破财之兆。",
"id": "b756fc5b81ec5229bf2b846636c15027",
"title": "与美女交往"
}
]
}
* 大家都看了 是返回了三条字符串,怎么展示呢 我在后面用for循环不知道怎么就失败了
*
* */
//json解析 我就不讲解了,如果不懂看我写的前几篇帖子
JSONObject o1 = new JSONObject(response);
int error_code = o1.getInt("error_code");
if (error_code == 0) {
//建立一个集合填充数据
List<DreamResult> list = new ArrayList<DreamResult>();
JSONArray j1 = o1.getJSONArray("result");
for (int i = 0; i < j1.length(); i++) {
JSONObject o2 = j1.getJSONObject(i);
DreamResult dreamResult = new DreamResult();
dreamResult.setDes(o2.getString("des"));
dreamResult.setTitle(o2.getString("title"));
dreamResult.setId(o2.getString("id"));
list.add(dreamResult);
}
if (list.size()>=0) {
title_one.setText(list.get(0).getTitle());
content_one.setText(list.get(0).getDes());
}
else if(list.size()>=1){
title_two.setText(list.get(1).getTitle());
content_two.setText(list.get(1).getDes());
}
else if(list.size()>=2) {
title_three.setText(list.get(2).getTitle());
content_three.setText(list.get(2).getDes());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void error(String error) {
}
});
}
}
点击下载源码