zoukankan      html  css  js  c++  java
  • http get post 请求

    1       建立Http连接的步骤:

    1.1      获得一个http的连接地址(如:String httpurl = "http://192.168.0.68:8090/Test/index.jsp?par=this-is-get-Method-request!";)

    1.2      构造一个URL对象(如:url = new URL(httpurl);)

    1.3      使用HttpURLConnection打开一个连接(如:HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();)

    1.4      得到读取的内容(流)(如:InputStream inputStream = httpConnection.getInputStream();)

    2     TestHttpGetPostActivity:

    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLEncoder;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class TestHttpGetPostActivity extends Activity
    {
    private Button mButton01;
    private Button mButton02;
    private Button mButton03;
    private String data = null;
    private TextView mTextView;
    private Intent intent = new Intent();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mButton01 = (Button) findViewById(R.id.get);
    mButton02 = (Button) findViewById(R.id.post);
    mButton03 = (Button) findViewById(R.id.nothing);
    mTextView = (TextView) findViewById(R.id.textview);
    mButton01.setOnClickListener(listener);
    mButton02.setOnClickListener(listener);
    mButton03.setOnClickListener(listener);
    }

    private OnClickListener listener = new OnClickListener()
    {
    @Override
    public void onClick(View v)
    {
    switch (v.getId())
    {
    // get请求
    case R.id.get:
    try
    {
    String httpurl = "http://192.168.0.68:8090/Test/index.jsp?par=this-is-get-Method-request!";
    URL url;
    url = new URL(httpurl);
    if (url != null)
    {
    // 使用HttpURLConnection打开网络连接
    HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
    InputStream inputStream = httpConnection.getInputStream();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    while (bufferedReader.readLine() != null)
    {
    data += bufferedReader.readLine() + "\n";
    }
    // 关闭inputStream
    inputStream.close();
    // 关闭http连接
    httpConnection.disconnect();
    System.out.println("data = " + data);
    if (data != null)
    {
    intent.putExtra("data", data);
    intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);
    startActivity(intent);
    }
    else
    {
    mTextView.setText("data is NULL!");
    }
    }
    }
    catch (MalformedURLException e)
    {
    e.printStackTrace();
    }
    catch (IOException e)
    {
    mTextView.setText("url is NULL!");
    e.printStackTrace();
    }
    break;

    // post请求
    case R.id.post:
    try
    {
    String httpurl = "http://192.168.0.68:8090/Test/index.jsp";
    URL url;
    url = new URL(httpurl);
    if (url != null)
    {
    // 使用HttpURLConnection打开网络连接
    HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
    // 因为是post请求,需要设置成true
    httpConnection.setDoInput(true);
    httpConnection.setDoOutput(true);
    // 设置post请求方式
    httpConnection.setRequestMethod("POST");
    // post请求不能使用缓存
    httpConnection.setUseCaches(false);
    httpConnection.setInstanceFollowRedirects(true);

    // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded
    httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成
    // 要注意的是connection.getOutputSream会隐含地进行connect.urlConn.connect();
    // DataOutputSream流
    DataOutputStream outputStream = new DataOutputStream(httpConnection.getOutputStream());
    // 要上传的参数
    String content = "par=" + URLEncoder.encode("this is post request!", "gb2312");
    // 将要上传的内容写入流中
    outputStream.writeBytes(content);
    // 刷新,关闭
    outputStream.flush();
    outputStream.close();
    InputStream inputStream = httpConnection.getInputStream();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    while (bufferedReader.readLine() != null)
    {
    data += bufferedReader.readLine() + "\n";
    }
    // 关闭inputStream
    inputStream.close();
    // 关闭http连接
    httpConnection.disconnect();
    System.out.println("data = " + data);
    if (data != null)
    {
    intent.putExtra("data", data);
    intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);
    startActivity(intent);
    }
    else
    {
    mTextView.setText("data is NULL!");
    }
    }
    }
    catch (MalformedURLException e)
    {
    e.printStackTrace();
    }
    catch (IOException e)
    {
    mTextView.setText("url is NULL!");
    e.printStackTrace();
    }
    break;
    case R.id.nothing:
    try
    {
    String httpurl = "http://192.168.0.68:8090/Test/index.jsp";
    URL url;
    url = new URL(httpurl);
    if (url != null)
    {
    // 使用HttpURLConnection打开网络连接
    HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
    InputStream inputStream = httpConnection.getInputStream();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    while (bufferedReader.readLine() != null)
    {
    data += bufferedReader.readLine() + "\n";
    }
    // 关闭inputStream
    inputStream.close();
    // 关闭http连接
    httpConnection.disconnect();
    System.out.println("data = " + data);
    if (data != null)
    {
    intent.putExtra("data", data);
    intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);
    startActivity(intent);
    }
    else
    {
    mTextView.setText("data is NULL!");
    }
    }
    }
    catch (MalformedURLException e)
    {
    e.printStackTrace();
    }
    catch (IOException e)
    {
    mTextView.setText("url is NULL!");
    e.printStackTrace();
    }
    break;
    }
    }
    };
    }

    3       ShowHtmlContent:

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;

    public class ShowHtmlContent extends Activity
    {
    private TextView mTextView;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.http);
    mTextView = (TextView) findViewById(R.id.textview);
    Intent intent = ShowHtmlContent.this.getIntent();
    String dataString = intent.getStringExtra("data");
    if (dataString != null)
    {
    mTextView.setText(dataString);
    }
    else
    {
    mTextView.setText("nothing to show!");
    }
    }
    }

    4    效果图

         




  • 相关阅读:
    VTK 体绘制讨论_光照&阴影、VTKLODProp3D
    VTK 体绘制讨论_颜色传输函数
    VTK 体绘制讨论_梯度不透明度传输函数
    VTK 体绘制讨论_不透明度传输函数
    VTK 体绘制裁剪_Cripping技术
    VTK 体绘制裁剪_Cropping技术
    VTK 纹理映射体绘制_三维纹理映射
    VTK 纹理映射体绘制_二维纹理映射
    VTK 体绘制_固定点光线投影体绘制与GPU加速光线投影体绘制
    VTK 体绘制_光线投影+最大密度投影+等值面法
  • 原文地址:https://www.cnblogs.com/jh5240/p/2311060.html
Copyright © 2011-2022 走看看