Android之长网址转换为短网址——百度短网址转换

来源:互联网 发布:c 编写windows程序 编辑:程序博客网 时间:2024/05/02 04:25

百度短网址转换需要用到post请求 具体代码如下

package com.example.myservice;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Looper;
import android.os.Message;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements Callback {
    private TextView test;
    private String getsucess;
    private Handler handler;
    String longUrl = "http://blog.csdn.net/lonely_fireworks/article/details/26244455";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Intent newIntent = new Intent(MainActivity.this, MService.class);
        // startService(newIntent);
        test = (TextView) findViewById(R.id.wsy);
        handler = new Handler(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 使用JDK中HttpURLConnection访问网络资源
                executeHttpPost(longUrl);
            }
        }).start();
    }

    public String executeHttpPost(String murl) {
        String result = null;
        URL url = null;
        HttpURLConnection connection = null;
        InputStreamReader in = null;
        try {
            url = new URL("http://dwz.cn/create.php");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            connection.setRequestProperty("Charset", "utf-8");
            DataOutputStream dop = new DataOutputStream(
                    connection.getOutputStream());
            // 设置post的参数
            dop.writeBytes("url=" + murl);
            dop.flush();
            dop.close();

            in = new InputStreamReader(connection.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(in);
            StringBuffer strBuffer = new StringBuffer();
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                strBuffer.append(line);
            }

            //取得结果
            getsucess = strBuffer.toString();
            handler.sendEmptyMessage(2);
            Looper.prepare();
            Toast.makeText(MainActivity.this, strBuffer.toString(), 200).show();
            Looper.loop();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

    public String getJsonParams(String object, String tag) {
        try {
            if (object != null) {
                JSONObject jsonObject = new JSONObject(object);
                return jsonObject.get(tag).toString();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean handleMessage(Message msg) {
        test.setText(getJsonParams(getsucess, "tinyurl"));
        return false;
    }

}

 

0 0
原创粉丝点击