volley 기능을 추상클래스로 만들고 -> Request
그 추상클래스를 상속받아 파라미터와 추가URL을 설정하는 클래스를 만들고 ->TestRequest
그 클래스의 객체를 만들어 실행하는 ->TestActivity
구조를 만들고 싶어서 만들고있는데...
되긴되지만 아직 미완성이다... 더 공부해야겠다
/////////////////////////////////////////////////////////////////
파라미터를 겟방식으로 url에 붙이는 것을 추가하는 것과
response 결과 데이터를 가공하는 것에대해 더 수정해야할것같다
비동기에대한 공부를 더해봐야겠다
//////////////////////////////////////////////////////////////////
Request.java
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import org.json.JSONObject;
import java.util.HashMap;
public abstract class Request {
private HashMap<String, String> params = new HashMap<String, String>();
private ProgressDialog loadingDialog;
private Context context = null;
private String baseUrl = "http://11.22.33.44/hahaha/";
private String urlName;
private String resultString="";
public Request(Context context) {
this.context = context;
}
public void request(final VolleyCallback callback) {
RequestQueue queue = VolleyUtill.getInstance(context).getRequestQueue();
loadingDialog = ProgressDialog.show(context, "불러오는 중입니다..","Please wait..", true, false);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
JsonRequest.Method.GET, baseUrl+urlName, (String)null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("Response", response.toString());
resultString = response.toString();
callback.onSuccess(resultString);
loadingDialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Response_error", error.toString());
loadingDialog.dismiss();
}
}
);
// Get the ImageLoader through your singleton class.
//mImageLoader = VolleyUtill.getInstance(this).getImageLoader();
VolleyUtill.getInstance(context).addToRequestQueue(jsObjRequest);
}
public interface VolleyCallback{
void onSuccess(String result);
}
/*
public Response.Listener<JSONObject> createMyReqSuccessListener() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("Response", response.toString());
//String stringFromJson = parseJSON(response);
result = response.toString();
//setResultString(response.toString());
loadingDialog.dismiss();
}
};
}
public Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Response error","errorr!!!!!!!");
//result = "fail";
//setResultString(error.toString());
loadingDialog.dismiss();
}
};
}*/
public void clearParams() {
this.params = new HashMap<String, String>();
}
public void setParam(String key, String value) {
this.params.put(key, value);
}
public void setUrlName(String urlName) {
this.urlName = urlName;
}
}
TestRequest.java
import android.content.Context;
import android.util.Log;
//import com.~~~~~~~~~~~~~.Request;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.HashMap;
public class TestRequest extends Request {
private String result;
public TestRequest (Context context) {
super(context);
}
public void setParams(String userId) {
super.clearParams();
super.setParam("user_id", userId);
}
public void startRequest(final VolleyCallback callback ) {
setUrlName("select_test.php");
request(callback);
}
public HashMap<String, String> jsondata(String data){
HashMap<String, String> resData = new HashMap<String, String>();
try{
JSONObject jsonobj = new JSONObject(data);
JSONArray jsonary = jsonobj.getJSONArray("result");
for(int i = 0; i < jsonary.length(); i++){
JSONObject obj = jsonary.getJSONObject(i);
resData.put("user_id", obj.getString("user_id"));
resData.put("test2", obj.getString("test2"));
resData.put("test3", obj.getString("test3"));
}
}catch (Exception e){
Log.d("json error ===", e.getMessage());
}
return resData;
}
}
TestActivity.java
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.util.HashMap;
/**
* Created by jo on 2016-03-19.
*/
public class BusActivity extends BaseActivity {
TextView mTextView;
private HashMap<String, String> busList = null;
@Override
protected void createActivity() {
View view = this.setContainerView(R.layout.activity_bus);
getSupportActionBar().setTitle("테스트");
mTextView = (TextView) view.findViewById(R.id.text);
onBusRequest();
}
private void onBusRequest(){
BusRequest request = new BusRequest(this);
request.setParams("23");
request.startRequest(new Request.VolleyCallback() {
@Override
public void onSuccess(String result) {
Log.i("hahahah", result);
}
});
}
@Override
protected void destroyActivity() {
}
@Override
protected void viewClick(View view) {
if (view.getId() == R.id.btn_back) {
this.finish();
}
}
}
'공부 > Android' 카테고리의 다른 글
(android) setimageresource null [src 이미지 비우기] (0) | 2016.05.11 |
---|---|
HoloColorPicker 값 가져오기 HEX값 (0) | 2016.05.11 |
(Android studio) Logcat 글자색 변경 (0) | 2016.05.04 |
(eclipse) svn plugin 설치 (0) | 2016.04.15 |
(eclipse) Eclipse ADT Plugin 설치 (0) | 2016.04.15 |