2、android studio加载外包compile 'com.lzy.net:okgo:2.1.4' compile 'com.lzy.net:okrx:0.1.2' compile 'com.lzy.net:okser...">
赞
踩
1、权限的设置
<uses-permission android:name="android.permission.INTERNET"/>
2、android studio加载外包
compile 'com.lzy.net:okgo:2.1.4'
compile 'com.lzy.net:okrx:0.1.2'
compile 'com.lzy.net:okserver:1.1.3'
3、写Mpplication
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
OkGo.init(this);
try {
OkGo.getInstance()
.debug("OkGo", Level.INFO, true)
.setConnectTimeout(60*1000)
.setReadTimeOut(60*1000)
.setWriteTimeOut(60*1000)
.setRetryCount(3);
}catch (Exception e)
{
e.printStackTrace();
}
}
}
4、在Manifest中添加Myapplication
android:name=".MyApplication"
5、简单的activity_main.xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/get"
android:text="开始解谜~~"/>
<ListView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
6、设置listview的xml
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="@android:color/black"
/>
<TextView
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/holo_red_light"
android:visibility="invisible"
/>
</LinearLayout>
7、MainActivity
public class MainActivity extends AppCompatActivity {
ArrayList<String> title = new ArrayList<>();
ArrayList<String> data = new ArrayList<>();
ArrayList<Integer> img = new ArrayList<>();
TextView da;
ListView show;
Button get;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (ListView)findViewById(R.id.show);
get = (Button)findViewById(R.id.get);
da = (TextView)findViewById(R.id.data
get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
title.clear();
data.clear();
img.clear();
String url = "https://api.tianapi.com/txapi/naowan/";
String param = "key=c0e68602d8a4b57045e6928b1b814a8d";
OkGo.get(url+"?"+param+"&num=10").execute(new StringCallback() {
@Override
public void onSuccess(String s, Call call, Response response) {
try {
JSONObject jsonObject=new JSONObject(s);
JSONArray array=jsonObject.getJSONArray("newslist");
for(int i=0;i<array.length();i++){
JSONObject json=array.getJSONObject(i);
String data1=json.getString("result");
String title1=json.getString("quest");
img.add(R.mipmap.ic_launcher_round);
title.add(title1);
data.add(data1);
}
List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
System.out.println("title:"+title.size());
for (int k=0;k<title.size();k++)
{
Map<String,Object> ls=new HashMap<String, Object>();
ls.put("header",img.get(k));
ls.put("data","答案:"+data.get(k));
ls.put("title",title.get(k));
System.out.println(data.get(k)+" "+title.get(k));
list.add(ls);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,list,R.layout.simple_item,
new String[]{"header","data","title"},new int[]{R.id.header,R.id.data,R.id.title});
show.setAdapter(simpleAdapter)
show.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"答案:"+data.get(position),Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
}
}
不想废话,有错误,请多指教!
关注我的技术公众号,每个工作日都有优质技术文章推送。
微信扫一扫下方二维码即可关注:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。