我知道有很多用于Android HTTP Get,Post,Put的库或API。但是有人可以告诉我哪个库更适合发布和放置以及其他各种库
答案 0 :(得分:2)
主要有两个用于网络相关操作的库,如发送接收数据或请求,响应e.t.c, 1)凌空抽射 2)改造
两者都是好的图书馆,两者都是灵活且易于管理的图书馆。
For Retrofit Click Here
对于齐射Click Here
答案 1 :(得分:1)
如果您想通过应用向数据库发出请求,我建议您使用Google为其制作的库:Volley
您可以找到允许您发出POST或GET请求的教程like this one。
您还可以通过排球
咨询Google的documentation答案 2 :(得分:1)
你应该使用排球,这是最好的事情,我在这里上传一些代码来帮助你。
<强> ApplicationController.java 强>
您必须在清单文件的应用程序标记
中添加此文件import android.app.Application;
import android.text.TextUtils;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.Volley;
public class ApplicationController extends Application {
public static final String TAG = "VolleyPatterns";
private static ApplicationController sInstance;
private RequestQueue mRequestQueue;
public static synchronized ApplicationController getInstance() {
return sInstance;
}
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
req.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
));
VolleyLog.d("Adding request to queue: %s", req.getUrl());
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
req.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
));
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
<强> CustomRequest.java 强>
自定义连接数据库并获取数据的请求。
import android.app.ProgressDialog;
import android.content.Context;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import raghav.bookthat.support.Constants;
public class CustomRequest {
static final String[] response = new String[1];
private StringRequestCompleteListener stringRequestCompleteListener;
public CustomRequest(StringRequestCompleteListener stringRequestCompleteListener) {
this.stringRequestCompleteListener = stringRequestCompleteListener;
}
public static String getResponse(String response) throws JSONException {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getString("Error").equals("200")) {
return jsonObject.getString("response");
} else {
return null;
}
}
public void loadJSON(final HashMap<String, String> headers, final Context context, final ProgressDialog progressDialog) {
if (headers != null) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.WEBSERVICE, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
try {
response[0] = getResponse(s);
if (stringRequestCompleteListener != null && response[0] != null) {
stringRequestCompleteListener.onTaskCompleted(response[0]);
} else {
if (progressDialog != null) {
progressDialog.dismiss();
}
}
} catch (JSONException e) {
if (progressDialog != null) {
progressDialog.dismiss();
}
response[0] = null;
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
if (volleyError instanceof TimeoutError) {
Toast.makeText(context, "Please Wait\n The Internet is Quite Slow Down Here", Toast.LENGTH_SHORT).show();
loadJSON(headers, context, progressDialog);
} else {
if (progressDialog != null) {
progressDialog.dismiss();
}
}
}
}) {
@Override
public Map<String, String> getParams() throws AuthFailureError {
return headers;
}
@Override
public Priority getPriority() {
return Priority.IMMEDIATE;
}
};
ApplicationController.getInstance().addToRequestQueue(stringRequest);
}
}
}
<强> StringRequestCompleteListener.java 强>
侦听员处理凌空的反应
public interface StringRequestCompleteListener {
void onTaskCompleted(String response);
}
使用自定义请求
HashMap<String, String> headers = new HashMap<>();
headers.put("method", "createBooking");
headers.put("PackageID", String.valueOf(bundle.getInt("PackageID")));
headers.put("UserID", String.valueOf(sharedPreferencesHelper.getUserID()));
headers.put("Price", txtTotalPrice.getText().toString());
headers.put("TotalPerson", edTotalPerson.getText().toString());
headers.put("BookingStartDateTime", txtStartDateTime.getText().toString());
headers.put("BookingEndDateTime", txtEndDateTime.getText().toString());
final ProgressDialog progress = SupportMethods.showProgressDialog(context);
progress.show();
new CustomRequest(new StringRequestCompleteListener() {
@Override
public void onTaskCompleted(String response) {
progress.dismiss();
if (response.equals("800")) {
Toast.makeText(context, "Location Already Booked On Given Time, \n Please Select Any Other Time Or Location", Toast.LENGTH_LONG).show();
} else if (response.equals("400")) {
Toast.makeText(context, "Error Occurred While Booking, Please Try Again", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Location Booked Successfully", Toast.LENGTH_LONG).show();
startActivity(new Intent(context, ActivityDrawer.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
startService(new Intent(context, BackgroundUpdateService.class).putExtra(Constants.UPDATE,Constants.BOOKING_UPDATE));
}
}
}).loadJSON(headers, context, progress);
<强>的AndroidManifest.xml 强>
添加此权限
<uses-permission android:name="android.permission.INTERNET" />
将ApplicationController添加到Application Tag
<application
android:name=".support.volley.ApplicationController"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme"><application>
答案 3 :(得分:0)
你用它来做Http .. httpclient-4.2.3.jar Download Link For Httpclient-4.2.3.jar
apache-httpcomponents-httpcore.jar Download Link for apche-httpcomponents