苦しい学習チャットロボットAPIの使用

5139 ワード

勉强ができなくなったような気がします.最近、自分の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="" />