日志文章

2020-1-11 aflfte2011

post获取网络信息方法

///工具类用于实现post方法
package com.aflfte.mylistapi;

import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MyModel {
    public static String post(String json,String tourl){
        String result="";
        BufferedReader reader=null;
        try{
            URL url=new URL(tourl);
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setRequestProperty("Connection","Keep-Alive");
            connection.setRequestProperty("Charset","UTF-8");
            connection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
            connection.setRequestProperty("accept","application/json");
            if(json!=null&&!TextUtils.isEmpty(json)){
                byte[] writebytes=json.getBytes();
                connection.setRequestProperty("Content-Length",String.valueOf(writebytes.length));
                OutputStream outputStream=connection.getOutputStream();
                outputStream.write(json.getBytes());
                outputStream.flush();
                outputStream.close();
                Log.d("httppost", "post: "+connection.getResponseCode());
            }
            if(connection.getResponseCode()==200){
                reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
                result=reader.readLine();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try{
                    reader.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

主线程类
package com.aflfte.mylistapi;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ArrayList<HashMap<String,String>> list=new ArrayList<>();
        setContentView(R.layout.activity_main);
        TextView textView=findViewById(R.id.hello);
        new Thread(new Runnable() {//利用线程获取网络数据
            @Override
            public void run() {
                String result=MyModel.post("{\"a\",\"a\"}","http://192.168.116.128/api.php?do=list");
                try{
                    JSONObject js=new JSONObject(result);//分析读取数据信息
                    String a=js.getString("a");
                    String k=js.getString("k");
                    String[] ids=a.split(",");
                    String[] keywords=k.split(",");
                    for(int i=0;i<ids.length;i++){
                        HashMap<String,String> hashMap=new HashMap<>();
                        hashMap.put("keyword_id",ids[i]);
                        hashMap.put("keyword",keywords[i]);
                        list.add(hashMap);
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }).start();
        SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.keywords,new String[]{"keyword_id","keyword"},new int[]{R.id.keyword_id,R.id.keyword});
        setListAdapter(adapter);//更新列表信息
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {//列表点击事件实现方法
        super.onListItemClick(l, v, position, id);
        Toast.makeText(getApplicationContext(),position+"->"+id,Toast.LENGTH_LONG).show();
    }
}

« AlertDialog提示消息的使用方法 | 子线程获取网络数据并更新UI线程方法»