package com.example.tuling; import android.os.AsyncTask; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; /** * Created by Administrator on 2016/11/7. */ public class HttpData extends AsyncTask<String,Void,String> { private HttpClient mHttpClient; private HttpGet mHttpGet; private HttpResponse mHttpResponse; private HttpEntity mHttpEntity; private InputStream in; private HttpGetDataListener listener; private String url; public HttpData(String url,HttpGetDataListener listener){ this.url=url; this.listener=listener; } @Override protected String doInBackground(String... params) { try { mHttpClient=new DefaultHttpClient(); mHttpGet=new HttpGet(url); mHttpResponse=mHttpClient.execute(mHttpGet); mHttpEntity=mHttpResponse.getEntity(); in=mHttpEntity.getContent(); BufferedReader br=new BufferedReader(new InputStreamReader(in)); String line=null; StringBuffer sb=new StringBuffer(); while ((line=br.readLine())!=null){ sb.append(line); } return sb.toString(); }catch (Exception e){ } return null; } @Override protected void onPostExecute(String s) { listener.getDataUrl(s); super.onPostExecute(s); } }
package com.example.tuling; /** * Created by Administrator on 2016/11/7. */ public interface HttpGetDataListener { void getDataUrl(String data); }
package com.example.tuling; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import org.json.JSONException; import org.json.JSONObject; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class MainActivity extends AppCompatActivity implements HttpGetDataListener,View.OnClickListener{ private HttpData httpData; private List<ListData> lists; private ListView lv; private EditText sendtext; private Button send_btn; private String content_str; private TextAdapter adapter; private String[] welcome_array; private double currentTime,oldTime=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView(){ lv= (ListView) findViewById(R.id.lv); sendtext= (EditText) findViewById(R.id.sendText); send_btn= (Button) findViewById(R.id.send_btn); lists=new ArrayList<ListData>(); send_btn.setOnClickListener(this); adapter=new TextAdapter(lists,this); lv.setAdapter(adapter); ListData listData; listData=new ListData(getRandomWelcomeTips(),ListData.RECEIVER,getTime()); lists.add(listData); } //随机欢迎语 private String getRandomWelcomeTips(){ String welcome_tip=null; welcome_array=this.getResources().getStringArray(R.array.welcome_tips); int index= (int) (Math.random()*(welcome_array.length-1)); welcome_tip=welcome_array[index]; return welcome_tip; } @Override public void getDataUrl(String data) { Log.e("TAG",data); parseText(data); } //JSON解析 public void parseText(String str){ try { JSONObject jb=new JSONObject(str); Log.e("TAG",jb.getString("code")); Log.e("TAG",jb.getString("text")); ListData listData; listData=new ListData(jb.getString("text"),ListData.RECEIVER,getTime()); lists.add(listData); adapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onClick(View v) { getTime(); content_str=sendtext.getText().toString(); sendtext.setText(""); String dropk=content_str.replace(" ",""); String droph=dropk.replace("\n",""); ListData listData; listData=new ListData(content_str,ListData.SEND,getTime()); lists.add(listData); if (lists.size()>30){ for (int i=0;i<lists.size();i++){ lists.remove(i); } } adapter.notifyDataSetChanged(); httpData= (HttpData) new HttpData("http://www.tuling123.com/openapi/api?key=343e6c6802e8494294faa56e3d45adfb&info="+droph,this).execute(); } //获取时间 private String getTime(){ currentTime=System.currentTimeMillis(); SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Date curDate=new Date(); String str=format.format(curDate); if (currentTime-oldTime>=1*60*1000){ oldTime=currentTime; return str; }else { return ""; } } }
package com.example.tuling; /** * Created by Administrator on 2016/11/7. */ public class ListData { public static final int SEND=1; public static final int RECEIVER=2; private String content; private int flag; private String time; public ListData(String content, int flag,String time) { setContent(content); setFlag(flag); setTime(time); } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public int getFlag() { return flag; } public void setFlag(int flag) { this.flag = flag; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
package com.example.tuling; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.RelativeLayout; import android.widget.TextView; import java.util.List; /** * Created by Administrator on 2016/11/7. */ public class TextAdapter extends BaseAdapter { private List<ListData> lists; private Context mContext; private RelativeLayout layout; public TextAdapter(List<ListData> lists,Context mContext) { this.lists = lists; this.mContext=mContext; } @Override public int getCount() { return lists.size(); } @Override public Object getItem(int position) { return lists.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater=LayoutInflater.from(mContext); if (lists.get(position).getFlag()==ListData.RECEIVER){ layout= (RelativeLayout) inflater.inflate(R.layout.left_item,null); } if (lists.get(position).getFlag()==ListData.SEND){ layout= (RelativeLayout) inflater.inflate(R.layout.right_item,null); } TextView tv= (TextView) layout.findViewById(R.id.tv); TextView time= (TextView) layout.findViewById(R.id.time); tv.setText(lists.get(position).getContent()); time.setText(lists.get(position).getTime()); return layout; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.tuling.MainActivity"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:transcriptMode="alwaysScroll" android:divider="@null" android:listSelector="@android:color/transparent" ></ListView> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/sendText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:id="@+id/send_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送"/> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/time" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"/> <ImageView android:layout_below="@+id/time" android:id="@+id/iv" android:layout_width="70dp" android:layout_height="70dp" android:padding="10dp" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/tv" android:layout_below="@+id/time" android:layout_marginRight="50dp" android:layout_toRightOf="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:background="@drawable/wangzai"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/time" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"/> <ImageView android:layout_alignParentRight="true" android:layout_below="@+id/time" android:id="@+id/iv" android:layout_width="70dp" android:layout_height="70dp" android:padding="10dp" android:src="@drawable/user"/> <TextView android:id="@+id/tv" android:layout_below="@+id/time" android:layout_marginLeft="50dp" android:layout_toLeftOf="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textColor="#f00f" android:background="@drawable/wangzai"/> </RelativeLayout>