安卓POST请求+异步获取数据+JSON解析+图片解析
来源:互联网 发布:网络卫星电视在线直播 编辑:程序博客网 时间:2024/04/29 10:16
**请大家关注下我的微信公众号:哦哦猿
QQ群:552123831**
我们今天写了一篇很简单的POST请求,fastjson的解析demo,里面包括了如何显示网络图片的demo。
这样请求的方式很简单,适合新手用,但是它的缺点就是实体类比较多,很麻烦。
建一个类:初始化 MyApplication
package com.ysbl.application;import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import com.nostra13.universalimageloader.core.assist.QueueProcessingType;import android.app.Application;import android.content.Context;public class MyApplication extends Application { public static ImageLoader imageLoader=ImageLoader.getInstance(); @Override public void onCreate() { ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .enableLogging() // Not necessary in common .build(); ImageLoader.getInstance().init(config); super.onCreate(); }//缓存图片 public static void initImageLoader(Context context) { ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( context).threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging() .build(); ImageLoader.getInstance().init(config); }}
建一个类: ScodeRegisdatas (这个类是用来解析的,参数是服务器给你的参数)
package com.ysbl.data.registers;public class ScodeRegisdatas { String sms_code; public String getSms_code() { return sms_code; } public void setSms_code(String sms_code) { this.sms_code = sms_code; }}
建一个类: Somall_HttpUtils(统一管理你的URL【就是你的接口,方便修改】)
package com.example.http;public class Somall_HttpUtils { public static String login= "http://rmgr.5ifacai.com/users/create_user"; //这个是我们公司的接口}
建一个类: Somall_Httppost (这个是用于POST请求的时候给服务器上传参数)
package com.example.http;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;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.apache.http.protocol.HTTP;public class Somall_Httppost { // 上传账号密码验证码 public static InputStream phoneHttpPostDatas(String path, String phone ) { HttpPost post = new HttpPost(path); List<NameValuePair> param = new ArrayList<NameValuePair>(); param.add(new BasicNameValuePair("phone", phone)); try { post.setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8)); HttpResponse response = new DefaultHttpClient().execute(post); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); return entity.getContent(); } } catch (Exception e) { e.printStackTrace(); } return null; } //这个方法不用去管它,它是用来读取返回值的。 public static String getData(InputStream inputStream) { String str = ""; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int len = 0; byte[] data = new byte[1024]; try { while ((len = inputStream.read(data)) != -1) { outputStream.write(data, 0, len); } str = new String(outputStream.toByteArray()); } catch (Exception e) { e.printStackTrace(); } return str; }}
在Mian中:
package com.example.http_post_json;import java.io.InputStream;import com.alibaba.fastjson.JSON;import com.example.http.Somall_HttpUtils;import com.example.http.Somall_Httppost;import com.nostra13.universalimageloader.core.DisplayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;import com.ysbl.data.registers.ScodeRegisdatas;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { ScodeRegisdatas ScodeRegisdata; String mobile; TextView txt_one; ImageView ima_one; //这个是你图片解析时候用到的参数 protected ImageLoader imageLoader = ImageLoader.getInstance(); protected DisplayImageOptions options; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt_one=(TextView)findViewById(R.id.txt_one); ima_one=(ImageView)findViewById(R.id.ima_one); new yanzhenmaTask().execute(""); } // 获取验证码 private class yanzhenmaTask extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... arg0) { try { mobile="13798426666"; InputStream inputStream = Somall_Httppost .phoneHttpPostDatas(Somall_HttpUtils.login, mobile); String result = Somall_Httppost.getData(inputStream); return result; } catch (Exception e) { e.printStackTrace(); } return ""; } protected void onPostExecute(String result) { super.onPostExecute(result); String nulls=result; if (nulls!="") { String xinwen = result; Log.e("返回值是:", xinwen); ScodeRegisdata = JSON.parseObject(result, ScodeRegisdatas.class); txt_one.setText("返回值是:"+ScodeRegisdata.getSms_code()); //参数一 你地址的URL,参数二你的图片控件名称 imageLoader.displayImage("http://img4.imgtn.bdimg.com/it/u=347254090,3099180210&fm=11&gp=0.jpg",ima_one,options, null); } } }}
记住图片解析要初始化:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.http_post_json" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> //添加网络权限 <uses-permission android:name="android.permission.INTERNET" /> <application //记住这里要初始化 初始化 初始化 android:name="com.ysbl.application.MyApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
这里我们要用到两个 jar包。
第一个是json解析包,第二个是图片解析包
**请大家关注下我的微信公众号:哦哦猿
QQ群:552123831**
0 2
- 安卓POST请求+异步获取数据+JSON解析+图片解析
- 异步POST请求解析JSON
- Android异步线程OkHttp Post请求Json数据并解析
- Android异步线程OkHttp Post请求Json数据并解析
- POST获取数据和json解析
- 安卓json数据解析
- post请求和json解析
- iOS----POST异步请求解析数据时bug解决办法
- jquery 解析post请求返回得json数据
- javaweb使用get与post请求json数据并解析
- Struts2 Action接收POST请求JSON数据及其实现解析
- post请求获取json数据
- 同步异步网络请求封装以及数据JSON解析
- jquery解析JSON数据,异步请求,快速准确处理
- ajax同步异步请求与JSON数据解析
- struts2+jquery+Ajax异步请求发送与解析json数据
- struts2+jquery+Ajax异步请求发送与解析json数据
- 学习json post请求和json解析
- 24. Swap Nodes in Pairs
- Django REST 异常处理
- SQL 语句类型以及处理
- 网易2016杭研公共技术部安卓开发实习生面试
- (OK) 调试cBPM—CentOS7—gdb—gdbserver—问题的解决—1—手机死机
- 安卓POST请求+异步获取数据+JSON解析+图片解析
- java多线程-使用ReadWriteLock同步数据访问
- 使用Memory Analyzer tool(MAT)分析内存泄漏(一)
- CentOS 6 安装VMWare tools,以及解决安装后/mnt中有hgfs但没共享文件的方法
- CentOS7 中使用 QQ—VirtualBox + WinXP + QQ—摄像头的问题
- iOS 半透明 模糊化
- Java遍历一个目录下的所有文件
- (OK) 编译cBPM-android—CentOS 7—NDK8—androideabi-4.7—API14—2版
- 如何在.Net的C#中制作DLL文件