我拥有什么 ::我有一个简单的Asynctask,它使用Http来获取JSON
我想学习的内容 ::如何将其转换为使用retrofit
库的项目
我已经提到了下面的课程
SplashActivity.java
public class SplashActivity extends Activity {
/** Called when the activity is first created. */
JSONObject jsonobject;
JSONArray jsonarray;
ArrayList<HashMap<String, String>> arraylist;
private String Content;
DatabaseHandler db;
//TextView txtSplashTitle,txtSplashDesc;
@InjectView(R.id.txtSplashDesc) TextView txtSplashDesc=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);//using ButterKnife library for viewInjection
String serverURL = "";
db = new DatabaseHandler(this);
new LongOperation().execute(serverURL);
freeMemory();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//Setting fonts for textviews
setCustomFontForTextViews();
}
private void setCustomFontForTextViews() {
Typeface typeFace = Typeface.createFromAsset(getAssets(), "royalacid.ttf");
txtSplashDesc.setTypeface(typeFace);
}
// Class with extends AsyncTask class
private class LongOperation extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(SplashActivity.this);
protected void onPreExecute() {
// NOTE: You can call UI Element here.
Dialog.setMessage("Downloading source..");
Dialog.show();
}
// Call after onPreExecute method
protected Void doInBackground(String... urls) {
try {
// NOTE: Don't call UI Element here.
HttpGet httpget = new HttpGet("http://10.0.2.2:3009/findmybuffet/?storedproc=get_app_tables&flag=sudhakar");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
jsonobject = new JSONObject(Content);
jsonobject = jsonobject.getJSONObject("findmybuffet");
jsonarray = jsonobject.getJSONArray("buffets");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("buf_off_id", jsonobject.getString("buf_off_id"));
map.put("from_time", jsonobject.getString("from_time"));
map.put("to_time", jsonobject.getString("to_time"));
map.put("online_price", jsonobject.getString("online_price"));
map.put("reserved_price", jsonobject.getString("reserved_price"));
map.put("buf_image", jsonobject.getString("buf_image"));
map.put("res_name", jsonobject.getString("res_name"));
map.put("rating", jsonobject.getString("rating"));
map.put("latitude", jsonobject.getString("latitude"));
map.put("longitude", jsonobject.getString("longitude"));
map.put("buf_type_name", jsonobject.getString("buf_type_name"));
map.put("from_date", jsonobject.getString("from_date"));
map.put("to_date", jsonobject.getString("to_date"));
map.put("city_id", jsonobject.getString("city_id"));
map.put("city_name", jsonobject.getString("city_name"));
map.put("meal_type_id", jsonobject.getString("meal_type_id"));
map.put("meal_type_name", jsonobject.getString("meal_type_name"));
map.put("buf_desc", jsonobject.getString("buf_desc"));
map.put("distance", jsonobject.getString("distance"));
Log.d("----$$$----", map.toString());
//Calling database
db.addContact(map);
//findCityName();
//String result=getAddress(12.918286,77.669493);
// Log.d("----result$$$$$-----", result);
}
} catch (IOException|JSONException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
private String getAddress(double latitude, double longitude) {
StringBuilder result = new StringBuilder();
try {
Geocoder geocoder = new Geocoder(SplashActivity.this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
result.append(address.getLocality()).append("\n");
result.append(address.getCountryName());
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
return result.toString();
}
/*private void findCityName() {
// TODO Auto-generated method stub
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
}
*/
protected void onPostExecute(Void unused) {
// Close progress dialog
Dialog.dismiss();
}
}
private void freeMemory() {
jsonobject=null;
jsonarray=null;
arraylist=null;
Content=null;
}
}
没有错误,但我怎样才能转换为类似的改造项目
谢谢!
答案 0 :(得分:0)
您必须从http://square.github.io/retrofit/
获取JAR文件使用以下代码创建接口类(ClientInterface.java):
@GET("/findmybuffet/?storedproc=get_app_tables&flag={name}")
void findBuffet(@Path("name") String name, Callback cb);
实现将如下所示:
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer("http://10.0.2.2:3009")
.build();
ClientInterface service = restAdapter.create(ClientInterface.class);
Callback callback = new Callback() {
@Override
public void success(Object o, Response response) {
// Read response here
}
@Override
public void failure(RetrofitError retrofitError) {
// Catch error here
} };
service.findBuffet("sudhakar", callback);