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;
}
}
- Android之长网址转换为短网址——百度短网址转换
- 长网址转换为短网址
- php将长网址转换为短网址
- 长网站 转换为 短网址 的原理
- 网易短网址转换
- sina短网址转换
- python短网址转换
- 调用百度短网址API接口进行短网址的转换(Java程序的实现)
- 百度短网址
- 百度短网址接口
- 百度短网址接口
- iOS 分享长网址转短网址
- PHP实现长网址与短网址
- 短网址算法——之 10进制与62进制转换
- 短网址算法——之 10进制与62进制转换
- 短网址
- 短网址
- 短网址
- 好贴汇总
- wait,、WIFEXITED和WEXITSTATUS介绍
- java同步块的几个问题
- svn 遇到的死锁
- 计算机与ARM板通过路由器相连
- Android之长网址转换为短网址——百度短网址转换
- android 代码设置、打开wifi热点及热点的连接
- C# new 和 override的一些认知
- Java4android学习之对象导论
- post提交数据
- Bitmap 格式
- EF中数据优先,模型优先和代码优先
- 【JUnit】BeforeClass、AfterClass、Before与After示例
- [转载]Linux64位下jdk安装+tomcat安装