实例介绍
【实例简介】登陆新浪微博 并调用 新浪接口实现 基本的微博功能 (测试时 已经不能登陆新浪微博,可能是appkey失效了) 不过可以 参考其相关逻辑
【实例截图】
【实例截图】
【核心代码】
package com.example.anjoyosinaweibo; import java.io.ByteArrayOutputStream; import java.io.IOException; import org.json.JSONException; import org.json.JSONObject; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.location.Location; import android.location.LocationManager; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore.Images.Media; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.GridView; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.example.anjoyosinaweibo.adapter.EmotaionAdapter; import com.example.anjoyosinaweibo.api.PlaceAPI; import com.example.anjoyosinaweibo.api.StatusesAPI; import com.example.anjoyosinaweibo.util.Myspf; import com.example.anjoyosinaweibo.util.WeiboAutolinkUtil; import com.weibo.sdk.android.WeiboException; import com.weibo.sdk.android.net.RequestListener; public class SendWeiBoAct extends WeiboAct { EditText etcontent; Button btsend, btcancle; StatusesAPI api; ImageView send_shoseimgview; TextView tvaddress, tvclear; RelativeLayout center_relat; GridView gridView; public static final int GET_PIC = 101; String filepath = ""; Location location; String lat = ""; String lon = ""; LocationManager locationManager; /*软键盘管理服务**/ InputMethodManager inputmanger; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.send_layout); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); inputmanger=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); initview(); api = new StatusesAPI(accessToken); loadlistener(); etcontent.setText(Myspf.getMsgBox(con)); } private void initview() { etcontent = (EditText) findViewById(R.id.etweibocontent); btsend = (Button) findViewById(R.id.btsendweibo); btcancle = (Button) findViewById(R.id.btcancle); send_shoseimgview = (ImageView) findViewById(R.id.send_shoseimgview); tvaddress = (TextView) findViewById(R.id.send_tvlocation); tvclear = (TextView) findViewById(R.id.send_tvclear); center_relat=(RelativeLayout) findViewById(R.id.bottom_relative); gridView=(GridView) findViewById(R.id.emotions_gridview); EmotaionAdapter adapter=new EmotaionAdapter(this, MyApplication.getInstence().emotionlist); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { etcontent.getText().insert(etcontent.getSelectionStart(), gridView.getAdapter().getItem(position).toString()); etcontent.setText(WeiboAutolinkUtil.Autolink(etcontent.getText().toString(), con)); } }); } private void sendweibo() { if(etcontent.getText().toString().trim().length()<1){ ToastInfo("请输入微博内容");return; } if(filepath.length()<1){ api.update(etcontent.getText().toString(), lat, lon, new RequestListener() { @Override public void onIOException(IOException arg0) { } @Override public void onError(WeiboException arg0) { System.out.println(arg0.getMessage()); } @Override public void onComplete4binary(ByteArrayOutputStream arg0) { } @Override public void onComplete(String arg0) { handler.post(new Runnable() { @Override public void run() { ToastInfo("发送成功") ; finish(); } }); } }); } api.upload(etcontent.getText().toString(), filepath, lat, lon, new RequestListener() { @Override public void onIOException(IOException arg0) { } @Override public void onError(WeiboException arg0) { System.err.println(arg0); } @Override public void onComplete4binary(ByteArrayOutputStream arg0) { } @Override public void onComplete(String arg0) { handler.post(new Runnable() { @Override public void run() { ToastInfo("发送成功") ; finish(); } }); } }); } private void loadlistener() { tvclear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { etcontent.setText(""); } }); etcontent.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { tvclear.setText(140 - s.toString().length() ""); } }); btsend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sendweibo(); } }); btcancle.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //如果没有写要发送的数据 直接退出 if (etcontent.getText().toString() == null || etcontent.getText().toString().trim().length() < 1) { finish(); return; } new AlertDialog.Builder(con).setTitle("退出").setMessage("是否保存草稿?").setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Myspf.saveMsgBox(con, etcontent.getText().toString()); dialog.dismiss(); finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } }).create().show(); } }); etcontent.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { center_relat.setVisibility(View.GONE); } }); } public void doclick(View v) { switch (v.getId()) { case R.id.send_cose_camera: Intent in = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(in, GET_PIC); break; case R.id.send_cose_photo: /** 获取当前手机相册里面的图片 */ Intent intent = new Intent(Intent.ACTION_GET_CONTENT); /** 设置请求类型 */ intent.setType("image/*"); startActivityForResult(intent, GET_PIC); break; case R.id.send_cose_mention: break; case R.id.send_cose_emotion: ToastInfo("emtions"); center_relat.setVisibility(View.VISIBLE); //隐藏软键盘 inputmanger.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); break; case R.id.send_cose_more: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == GET_PIC) { Uri uri = data.getData(); if (uri == null) return; Cursor c = SendWeiBoAct.this.getContentResolver().query(uri, new String[] { Media.DATA }, null, null, null); if (c != null) { c.moveToFirst(); filepath = c.getString(c.getColumnIndex(Media.DATA)); c.close(); // 设置一个缩放 Options options = new Options(); options.inSampleSize = 4; send_shoseimgview.setImageBitmap(BitmapFactory.decodeFile(filepath, options)); } } } super.onActivityResult(requestCode, resultCode, data); } @Override protected void onResume() { getlocation(); super.onResume(); } /*** * 获取当前用户的位置信息 */ public void getlocation() { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location == null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); } if (location != null) { showaddress(); } else { ToastInfo("获取位置信息失败"); showaddress(); } } public void showaddress() { String coordinate = ""; if (location == null) { lat = "39.97948"; lon = "116.31635"; } else { lat = String.valueOf(location.getLatitude()); lon = String.valueOf(location.getLongitude()); } coordinate = lon "," lat; PlaceAPI api = new PlaceAPI(accessToken); api.parselocationtoGeo(coordinate, new RequestListener() { @Override public void onIOException(IOException arg0) { } @Override public void onError(WeiboException arg0) { } @Override public void onComplete4binary(ByteArrayOutputStream arg0) { } @Override public void onComplete(String arg0) { System.out.println(arg0 "位置信息"); try { JSONObject jsonObject = new JSONObject(arg0).getJSONArray("geos").getJSONObject(0); final String address = jsonObject.getString("address"); final String name; if (jsonObject.has("name")) { name = jsonObject.getString("name"); } else { name = ""; } System.out.println(address); // 解析完成 显示 handler.post(new Runnable() { @Override public void run() { tvaddress.setText(address "," name); } }); } catch (JSONException e) { e.printStackTrace(); } } }); } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论