艰苦的学习 聊天机器人 API的使用

感觉学习到一个上不着天下不着地的位置了 尴尬  最近有很多东西不理解 感觉自己java知识太薄弱  慢慢补吧  下面这个demo是使用了

聚合数据的  自动聊天机器人  的API 然后正好可以学习一下 URL 以及输入流输出流的处理  还还有JSON 

package com.imooc.admin.httpurlconnection;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

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.URL;

public class MainActivity extends AppCompatActivity {
    public static final int SHOW_RESPONSE=0;
    private Button get;
    private TextView show;
    private EditText input;
    String say=null;
    //新建一个Handler 用于接收和发送消息
    private android.os.Handler handler=new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SHOW_RESPONSE:
                    String response= (String) msg.obj;
                    String ans=parseJSONWithJSONObject(response);
                    show.setText(ans);
            }
        }


    };
    private String parseJSONWithJSONObject(String jsonDate) {
        String text = null;
        try {
                //新建的JSONObject中参数为返回的json字符,解析收到的JSON内容
                JSONObject jsonObject = new JSONObject(jsonDate);
                //解析result中的json内容
                JSONObject result=jsonObject.getJSONObject("result");
                text = result.getString("text");
            } catch (JSONException e1) {
            e1.printStackTrace();
        }

        return text;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        get= (Button) findViewById(R.id.button);
        show= (TextView) findViewById(R.id.textView);
        input= (EditText) findViewById(R.id.editText);

        get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendRequestWithHttpURLConnection();
            }

            private void sendRequestWithHttpURLConnection() {
                say=input.getText().toString();
                Toast.makeText(MainActivity.this, "输入的是"+say, Toast.LENGTH_SHORT).show();
                //开启线程来发起网络请求
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        //设置HttpURLConnection为空
                        HttpURLConnection connection=null;
                        try{
                            //给URL设置内容
                            URL url=new URL("http://op.juhe.cn/robot/index?info="+say+"&key=a35f1c3e459d6826fd3330daedc577a6");
                            //实例化HttpURLConnection
                            connection= (HttpURLConnection) url.openConnection();
                            //获取方式为GET从网络获得数据  POST为向网络发送数据
                            connection.setRequestMethod("GET");
                            //建立连接超过8秒就超时了
                            connection.setConnectTimeout(8000);
                            //读取数据超过8秒就超时了
                            connection.setReadTimeout(8000);
                            //建立一个输入流 接收从网页的到的输入
                            InputStream in=connection.getInputStream();
                            //对获取到的输入流进行读取
                            BufferedReader reader=new BufferedReader(new InputStreamReader(in));
                            StringBuilder response=new StringBuilder();
                            String line;
                            while ((line=reader.readLine())!=null){
                                response.append(line);
                            }
                            Message message=new Message();
                            message.what=SHOW_RESPONSE;
                            //将服务器返回的结果存放到Message中
                            message.obj=response.toString();
                            handler.sendMessage(message);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }finally {
                            //如果连接着  就断开~
                            if(connection!=null){
                                connection.disconnect();
                            }
                        }
                    }
                }).start();
            }
        });
    }
}
<img src="http://img.blog.csdn.net/20160402022251264?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


你可能感兴趣的:(android)