import android.graphics.Bitmap; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.yolanda.nohttp.NoHttp; import com.yolanda.nohttp.rest.CacheMode; import com.yolanda.nohttp.rest.Request; import com.yolanda.nohttp.rest.RequestQueue; import com.yolanda.nohttp.rest.Response; import com.yolanda.nohttp.rest.SimpleResponseListener; public class MainActivity extends AppCompatActivity { private Button btn1; private Button btn2; private TextView tv; private ImageView iv; private Button btn3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); tv = (TextView) findViewById(R.id.textView); iv = (ImageView) findViewById(R.id.imageView); btn3 = (Button) findViewById(R.id.btn3); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RequestQueue requestQueue = NoHttp.newRequestQueue(); Request<String> stringRequest = NoHttp.createStringRequest("http://www.163.com/"); requestQueue.add(102, stringRequest, new SimpleResponseListener<String>() { @Override public void onSucceed(int i, Response<String> response) { Toast.makeText(MainActivity.this, "onSucceed", Toast.LENGTH_SHORT).show(); tv.setText(response.get()); } @Override public void onFailed(int i, String s, Object o, Exception e, int i1, long l) { Toast.makeText(MainActivity.this, "onFailed" , Toast.LENGTH_SHORT).show(); tv.setText(e.getMessage()); } }); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RequestQueue requestQueue = NoHttp.newRequestQueue(); Request<Bitmap> bitmapRequest = NoHttp.createImageRequest("http://123.56.111.226:8080/tomcat.png"); requestQueue.add(101, bitmapRequest, new SimpleResponseListener<Bitmap>() { @Override public void onSucceed(int i, Response<Bitmap> response) { iv.setImageBitmap(response.get()); } @Override public void onFailed(int i, String s, Object o, Exception e, int i1, long l) { tv.setText(e.getMessage()); } }); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RequestQueue requestQueue = NoHttp.newRequestQueue(); Request<Bitmap> bitmapRequest = NoHttp.createImageRequest("http://123.56.111.226:8080/tomcat.png"); bitmapRequest.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE); requestQueue.add(101, bitmapRequest, new SimpleResponseListener<Bitmap>() { @Override public void onSucceed(int i, Response<Bitmap> response) { iv.setImageBitmap(response.get()); } @Override public void onFailed(int i, String s, Object o, Exception e, int i1, long l) { tv.setText(e.getMessage()); } }); } }); } }