Android,机器狗应用
源码如下:
package com.wyl.jqr; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import com.example.tulinjiqiren.R; import com.google.gson.Gson; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends ActionBarActivity { Button btn; Button btn2; EditText et; TextView tv; myHandler handler = new myHandler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button)findViewById(R.id.btn01); btn2 = (Button)findViewById(R.id.btn02); et = (EditText)findViewById(R.id.editText01); tv = (TextView)findViewById(R.id.textview02);//用来赋值 MyOnclickListener l = new MyOnclickListener(); btn.setOnClickListener(l); btn2.setOnClickListener(l); } class MyOnclickListener implements OnClickListener{ @Override public void onClick(View view) { if(view.getId()==R.id.btn01){ //1.获取录入的问题 String question = et.getText().toString(); //2.访问网络 //主线程不能够访问网络,由此,需要借助开启新线程和handler的方式 myThread myThread = new myThread(); myThread.start(); System.out.println("发送请求的线程:"+Thread.currentThread().getName()); }else if(view.getId()==R.id.btn02){ //清空edittext System.out.println("清楚的线程:"+Thread.currentThread().getName()); et.setText(""); } } } class myHandler extends Handler{ @Override public void handleMessage(Message msg) { String daan = (String)msg.obj; if(daan == "" || daan.equals(null)){ daan = "暫時不支持該轉換"; } System.out.println("myHandler()方法的线程:"+Thread.currentThread().getName()); tv.setText(daan); } } class myThread extends Thread{ @Override public void run() { //访问网络 System.out.println("run()的线程:"+Thread.currentThread().getName()); String APIKEY = "e59219b3f450256c2a3da7157ae651f2"; String INFO; String getURL; URL getUrl; String question = et.getText().toString();//获取前台的问题 try { INFO = URLEncoder.encode(question, "utf-8");//需要改造 getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO; getUrl = new URL(getURL); HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); connection.connect(); // 取得输入流,并使用Reader读取 BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8")); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); // 断开连接 connection.disconnect(); System.out.println("获取到的答案"+sb); //sb是json格式的,需要做轉化, Gson gson = new Gson(); String sb2 = sb.toString(); System.out.println("sb2的值:"+sb2); myBean rtn = gson.fromJson(sb2, myBean.class); System.out.println("rtn的值:"+rtn); String text = rtn.getText(); System.out.println("text的值:"+text); //因为本线程workThread,不能够直接把值返回到UI中,所以把答案放到队列里, Message msg = handler.obtainMessage(); msg.obj = text;//把轉化後的答案綁定到msg上 handler.sendMessage(msg); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
myBean.java
因为用到了Gson来解析,所以根据返回的json字符串的key来写一个bean。
package com.wyl.jqr; public class myBean { String code; String text; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getText() { return text; } public void setText(String text) { this.text = text; } }