android開源框架android-async-http使用
android-async-http開源框架可以是我們輕松的獲取網絡數據或者向服務器發送數據,使用起來也很簡單,下面做簡單介紹,具體詳細使用看官網:https://github.com/loopj/android-async-http
1.新建項目,去官網下載zip包,解壓,打開releases文件,把里面最新的jar包,考入項目工程libs目錄下,引入包。
2.通過1,就可以使用了,很簡單,下面是自己寫的demo,用它提供的各種不同方法完成從服務器獲取一個json數據:
package com.http; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.BinaryHttpResponseHandler; import com.loopj.android.http.JsonHttpResponseHandler; import com.loopj.android.http.RequestParams; public class HttpUtil { private static AsyncHttpClient client =new AsyncHttpClient(); //實例話對象 static { client.setTimeout(11000); //設置鏈接超時,如果不設置,默認為10s } public static void get(String urlString,AsyncHttpResponseHandler res) //用一個完整url獲取一個string對象 { client.get(urlString, res); } public static void get(String urlString,RequestParams params,AsyncHttpResponseHandler res) //url里面帶參數 { client.get(urlString, params,res); } public static void get(String urlString,JsonHttpResponseHandler res) //不帶參數,獲取json對象或者數組 { client.get(urlString, res); } public static void get(String urlString,RequestParams params,JsonHttpResponseHandler res) //帶參數,獲取json對象或者數組 { client.get(urlString, params,res); } public static void get(String uString, BinaryHttpResponseHandler bHandler) //下載數據使用,會返回byte數據 { client.get(uString, bHandler); } public static AsyncHttpClient getClient() { return client; } }
這個類主要列出了我們常用的get方法,在要使用的地方,調用該類就行了。
具體使用的類:
package com.http; import java.io.File; import java.io.FileOutputStream; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.TextView; import android.widget.Toast; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.BinaryHttpResponseHandler; import com.loopj.android.http.JsonHttpResponseHandler; import com.loopj.android.http.RequestParams; public class MainActivity extends Activity { private TextView textView; // 頂部textview private ProgressDialog pDialog; private TextView textView2; // 下面textview,顯示獲取的所有數據 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text); textView2 = (TextView) findViewById(R.id.text2); } public void method1(View view) { pDialog = ProgressDialog.show(this, "請稍等", "數據加載中"); String urlString = "http://client.azrj.cn/json/cook/cook_list.jsp?type=1&p=2&size=10"; // 一個獲取菜譜的url地址 HttpUtil.get(urlString, new AsyncHttpResponseHandler() { public void onSuccess(String arg0) { // 獲取數據成功會調用這里 pDialog.dismiss(); textView.setText("獲取json數據成功,看下面"); textView2.setText(arg0); Log.i("hck", arg0); }; public void onFailure(Throwable arg0) { // 失敗,調用 Toast.makeText(MainActivity.this, "onFailure", Toast.LENGTH_LONG).show(); }; public void onFinish() { // 完成后調用,失敗,成功,都要掉 }; }); } public void method2(View view) { String urlString = "http://client.azrj.cn/json/cook/cook_list.jsp?"; RequestParams params = new RequestParams(); // 綁定參數 params.put("type", "1"); params.put("p", "2"); params.put("size", "10"); HttpUtil.get(urlString, params, new JsonHttpResponseHandler() { public void onSuccess(JSONArray arg0) { // 成功后返回一個JSONArray數據 Log.i("hck", arg0.length() + ""); try { textView.setText("菜譜名字:" + arg0.getJSONObject(2).getString("name")); //返回的是JSONArray, 獲取JSONArray數據里面的第2個JSONObject對象,然后獲取名字為name的數據值 } catch (Exception e) { Log.e("hck", e.toString()); } }; public void onFailure(Throwable arg0) { Log.e("hck", " onFailure" + arg0.toString()); }; public void onFinish() { Log.i("hck", "onFinish"); }; public void onSuccess(JSONObject arg0) { //返回的是JSONObject,會調用這里 Log.i("hck", "onSuccess "); try { textView.setText("菜譜名字:" + arg0.getJSONArray("list").getJSONObject(0) .getString("name")); } catch (Exception e) { Log.e("hck", e.toString()); } }; }); } public void method3(View view) { String urlString = "http://client.azrj.cn/json/cook/cook_list.jsp?type=1&p=2&size=10"; HttpUtil.get(urlString, new JsonHttpResponseHandler() { public void onSuccess(JSONObject arg0) { try { textView.setText("菜譜名字:" + arg0.getJSONArray("list").getJSONObject(1) .getString("name")); } catch (Exception e) { Log.e("hck", e.toString()); } }; }); } public void method4(View view) { String urlString = "http://client.azrj.cn/json/cook/cook_list.jsp?"; final RequestParams params = new RequestParams(); params.put("type", "1"); params.put("p", "2"); params.put("size", "10"); HttpUtil.get(urlString, params, new AsyncHttpResponseHandler() { public void onSuccess(String arg0) { try { JSONObject jObject = new JSONObject(arg0); textView.setText("菜譜名字:" + jObject.getJSONArray("list").getJSONObject(2) .getString("name")); Log.i("hck", params.getEntity().toString()); } catch (Exception e) { } }; }); } public void method5(View view) { String url = "http://f.hiphotos.baidu.com/album/w%3D2048/sign=38c43ff7902397ddd6799f046dbab3b7/9c16fdfaaf51f3dee973bf7495eef01f3b2979d8.jpg"; HttpUtil.get(url, new BinaryHttpResponseHandler() { @Override public void onSuccess(byte[] arg0) { super.onSuccess(arg0); File file = Environment.getExternalStorageDirectory(); File file2 = new File(file, "cat"); file2.mkdir(); file2 = new File(file2, "cat.jpg"); try { FileOutputStream oStream = new FileOutputStream(file2); oStream.write(arg0); oStream.flush(); oStream.close(); textView.setText("可愛的貓咪已經保存在sdcard里面"); } catch (Exception e) { e.printStackTrace(); Log.i("hck", e.toString()); } } }); } }
布局文件:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal"> <TextView android:textColor="#FF0000" android:textSize="16sp" android:layout_marginTop="20dp" android:gravity="center_horizontal" android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獲取數據完成后會顯示在這里" /> <Button android:layout_marginTop="50dp" android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一種" android:onClick="method1" /> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第2種" android:onClick="method2" /> <Button android:id="@+id/bt3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第3種" android:onClick="method3" /> <Button android:id="@+id/bt4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第4種" android:onClick="method4" /> <Button android:id="@+id/bt5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第5種" android:onClick="method5" /> <TextView android:textColor="#FF0000" android:textSize="16sp" android:layout_marginTop="20dp" android:gravity="center_horizontal" android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView>
很簡單很實用,上面只是get方法的,上傳數據,和這個也差不多,大家自己建個服務器,試試。記得加入網文網絡的權限和向sdcard的訪問權限
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!