关于接口的初步练习
Posted 定不负相思懿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于接口的初步练习相关的知识,希望对你有一定的参考价值。
这是主函数
package com.example.com.webtext; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import org.json.JSONArray; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; import static com.example.com.webtext.R.id.button; public class MainActivity extends AppCompatActivity implements View .OnClickListener{ private List<massage > Mymassage=new ArrayList<>() ; TextView nameText; TextView summaryText; ImageView img; Button sendRequest; private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendRequest=(Button) findViewById(button) ; nameText =(TextView) findViewById(R.id.name ) ; summaryText =(TextView) findViewById(R.id.summery); img=(ImageView) findViewById(R.id.photo) ; sendRequest .setOnClickListener(this) ; } @Override public void onClick(View v ){ if(v.getId() == button){ HttpUtil .sendHttpRequest("https://qcloud.waydrow.com/LoveInn/index.php/Home/App/getInfoList",new HttpCallbackListener(){ @Override public void onFinish(String response){ //Log.d(TAG, "onFinish: "+response); parseJSONWithJSONObject(response ); } @Override public void onError(Exception e){ e.printStackTrace();; } }); } setRecyclerview(); } private void parseJSONWithJSONObject(String jsonData){ try{ JSONArray jsonArray =new JSONArray(jsonData ) ; for(int i=0;i<jsonArray .length() ;i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String name = jsonObject.getString("name"); String summary = jsonObject.getString("summary"); String imgUrl = "https://qcloud.waydrow.com" + jsonObject.getString("photo"); massage massage1 = new massage(name, summary, imgUrl); Mymassage.add(massage1); } } catch (Exception e){ e.printStackTrace() ; } } public void setRecyclerview(){ RecyclerView recyclerView =(RecyclerView ) findViewById(R.id.recycler_view); StaggeredGridLayoutManager LayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL) ; recyclerView.setLayoutManager(LayoutManager); adapter Adapter =new adapter(Mymassage ); recyclerView.setAdapter(Adapter); Mymassage =null ; } }
这是开启连接的类
package com.example.com.webtext; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; /** * Created by 定不负相思懿 on 2017/5/10. */ public class HttpUtil { public static void sendHttpRequest(final String adress,final HttpCallbackListener linsener){ new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection =null ; BufferedReader reader =null; try { URL url=new URL(adress); connection =(HttpURLConnection ) url.openConnection() ; connection .setRequestMethod("GET"); connection .setConnectTimeout(8000); connection .setReadTimeout(8000); connection .setDoInput(true); connection .setDoOutput(true); InputStream in=connection.getInputStream(); reader =new BufferedReader(new InputStreamReader(in)) ; StringBuilder response=new StringBuilder(); String line; while((line=reader .readLine()) !=null){ response .append(line) ; } if(linsener !=null){ linsener.onFinish(response .toString()); } } catch (ProtocolException e) { linsener.onError(e); } catch (MalformedURLException e) { linsener.onError(e); } catch (IOException e) { linsener.onError(e); } finally{ if(connection!=null ){ connection .disconnect(); } } } }).start(); ; } }
还有类和adapter
package com.example.com.webtext; /** * Created by 定不负相思懿 on 2017/5/11. */ public class massage { String name; String summary; String photo; public massage(String name, String summary, String photo){ this.name =name; this.summary =summary ; this.photo =photo; } public String getname(){ return name; } public String getsummary (){ return summary ; } public String getphoto(){ return photo; } }
package com.example.com.webtext; import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; /** * Created by 定不负相思懿 on 2017/5/11. */ public class adapter extends RecyclerView.Adapter<adapter .ViewHolder >{ private List<massage> mymassage; public adapter (List<massage> fruit3){ mymassage=fruit3; } static class ViewHolder extends RecyclerView.ViewHolder { ImageView image; TextView name; TextView summary; View fruitview; Context context1; public ViewHolder(View view) { super(view); fruitview =view; image=(ImageView ) view.findViewById(R.id.photo) ; name =(TextView ) view.findViewById(R.id.name) ; summary =(TextView ) view.findViewById(R.id.summery) ; } } @Override public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { View view= LayoutInflater.from(parent .getContext() ).inflate(R.layout.recyclerview_layout,parent ,false ) ; final ViewHolder holder=new ViewHolder(view ); holder.context1=parent .getContext() ; holder.fruitview.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ int position=holder.getAdapterPosition() ; massage massage =mymassage .get(position ); } }) ; return holder ; } @Override public void onBindViewHolder(ViewHolder holder, int position) { massage mymassage1=mymassage.get(position ); holder .name.setText(mymassage1 .getname() ) ; holder .summary .setText(mymassage1 .getsummary() ) ; Glide.with(holder .context1).load(mymassage1.getphoto()).into(holder.image ); } @Override public int getItemCount() { return mymassage .size() ; } }
最重要的!就是接口
public interface HttpCallbackListener { void onFinish(String response); void onError(Exception e); }
虽然看着接口很简单,但是...用起来就知道了。当你会的时候就会发现真的很方便,但是如果你不会的话,不如直接写函数。
以上是关于关于接口的初步练习的主要内容,如果未能解决你的问题,请参考以下文章