美文网首页
Android studio 封装POST请求

Android studio 封装POST请求

作者: BSKSdorica | 来源:发表于2021-01-08 16:32 被阅读0次

    新建UpdateTextTask.java文件 ---名称自定义,注意导包,事件自行修改。

    public class UpdateTextTask extends AsyncTask<Void,Integer,String> {
    private Context context;
    private String url;
    private String postValue;
    private TextView text;
    public UpdateTextTask(Context context,String string,String postValue,TextView text){
    this.context = context;
    this.url = string;
    this.postValue = postValue;
    this.text = text;
    }
    @Override
    protected void onPreExecute(){
    Toast.makeText(context,"执行",Toast.LENGTH_SHORT).show();
    // super.onPreExecute();
    }
    @Override
    protected String doInBackground(Void... params){
    int i=0;
    while(i<10){
    i++;
    publishProgress(i);
    try{
    Thread.sleep(1000);
    }catch(InterruptedException e){}
    }
    String result = Common.postGetJson(url,postValue);
    return result;
    }
    @Override
    protected void onPostExecute(String i){
    Toast.makeText(context,i,Toast.LENGTH_SHORT).show();
    text.setText(i);
    }
    @Override
    protected void onProgressUpdate(Integer... values){
    text.setText(""+values[0]);
    }
    }

    新建Common文件 ---自定义文件名,和上一个文件放一起,可创建一个utils文件夹。

    public class Common {
    public static String postGetJson(String url,String content){
    try{
    URL murl = new URL(url);
    HttpURLConnection httpURLConnection = (HttpURLConnection) murl.openConnection();
    httpURLConnection.setConnectTimeout(15000);
    httpURLConnection.setReadTimeout(15000);
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setRequestProperty("Connection","keep-alive");
    httpURLConnection.setRequestProperty("Content-Type","application/json");
    httpURLConnection.setRequestProperty("charset","UTF-8");
    httpURLConnection.setDoInput(true);
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setUseCaches(false);
    httpURLConnection.connect();
    DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
    System.out.println(content);
    String postContent = content;
    dos.write(postContent.getBytes());
    dos.flush();
    dos.close();
    int responseCode = httpURLConnection.getResponseCode();
    System.out.println(responseCode);
    if(responseCode == 200){
    InputStream is = httpURLConnection.getInputStream();
    ByteArrayOutputStream message = new ByteArrayOutputStream();
    int len = 0;
    byte buffer[] = new byte[1024];
    while((len=is.read(buffer))!=-1){
    message.write(buffer,0,len);
    }
    is.close();
    message.close();
    String msg = new String(message.toByteArray());
    Log.d("Common",msg);
    return msg;
    }
    return "fail";
    }catch(Exception e){
    return "error";
    }
    }
    }

    页面调用代码,注意和之前修改的名称一致。

    private String url = "..."; ---接口地址
    private void init(){ ---调用方法名
    try {
    String name = "abc";
    String pwd = "123";
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("name",name);
    jsonObject.put("pwd",pwd);
    String value = jsonObject.toString();
    UpdateTextTask updateTextTask = new UpdateTextTask(this,url,value,text);
    updateTextTask.execute();
    }catch(Exception e){
    text.setText("error");
    }
    }

    相关文章

      网友评论

          本文标题:Android studio 封装POST请求

          本文链接:https://www.haomeiwen.com/subject/tqefaktx.html