我是Web Services的新手。我想在Android中集成Web服务(REST)。任何人都可以建议一些好的教程。
答案 0 :(得分:4)
后端是什么? 如果是JAVA,那么您可以使用REST with Java (JAX-RS) using Jersey。
在Android方面,您可以使用this simple RestClient来处理该REST服务。
对于JSON< - >双方的对象映射(Android,Java后端)都可以使用GSON。
答案 1 :(得分:1)
我为HTTP POST和HTTP GET做的示例代码
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import android.os.AsyncTask;
import android.util.Log;
public class HttpConnectionHandler {
public static final String TAG = HttpConnectionHandler.class.getSimpleName();
private ServerRequestCommand requestCommand;
private HttpPost httpPost;
private HttpGet httpGet;
private HttpClient httpClient;
private HttpResponse httpResponse;
public HttpConnectionHandler(){
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
this.httpClient = new DefaultHttpClient(httpParameters);
//this.httpClient = new DefaultHttpClient();
}
private void doNetworkConnection(){
new AsyncServerConnector().execute();
}
class AsyncServerConnector extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0) {
try {
if (Utility.isWifiPresent()
|| Utility.isMobileConnectionPresent()) {
//Establish network connection here and set the response to request; if successful
//check GET or POST
if(requestCommand.getPostData() != null){
//means its POST
httpResponse = httpClient.execute(prepareHttpPost());
}else{
//its GET method
httpResponse = httpClient.execute(prepareHttpGet());
}
if(httpResponse != null){
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode == HttpStatus.SC_OK){
InputStream inputStream = httpResponse.getEntity().getContent();
String serverResponse = formatServerResponse(inputStream);
requestCommand.setServerResponse(serverResponse);
requestCommand.setState(RequestCommand.STATE_SUCCESS);
}else{
requestCommand.setState(RequestCommand.STATE_SERVER_FAILURE);
}
}else{
requestCommand.setState(RequestCommand.STATE_SERVER_FAILURE);
}
}
//No network
else{
requestCommand.setState(RequestCommand.STATE_NETWORK_FAILURE);
}
}
catch (IOException e) {
Log.i(TAG, "IOException during hitting for service: "+requestCommand.getServerUrl()+" and ID "+requestCommand.getRequestId());
requestCommand.setState(RequestCommand.STATE_SERVER_FAILURE);
}
catch (Exception e) {
Log.i(TAG, "Error during hitting for service: "+requestCommand.getServerUrl()+" and ID "+requestCommand.getRequestId());
requestCommand.setState(RequestCommand.STATE_SERVER_FAILURE);
}
return null;
}
protected void onPostExecute(String file_url) {
Log.i(TAG, "Get response for service: "+requestCommand.getServerUrl()+" and ID "+requestCommand.getRequestId());
NetworkHandler.receiveResponseFromServer(requestCommand);
requestCommand = null;
}
}
public HttpPost prepareHttpPost() throws UnsupportedEncodingException {
this.httpPost = new HttpPost("your url");
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new ByteArrayEntity(requestCommand.getPostData()
.getBytes("UTF8")));
return httpPost;
}
public HttpGet prepareHttpGet() {
this.httpGet = new HttpGet(requestCommand.getServerUrl());
return httpGet;
}
private String formatServerResponse(InputStream inputStream) throws IOException {
String serverResponse = null;
StringBuffer responseBuffer = new StringBuffer();
byte[] data = new byte[256];
int len = 0;
int size = 0;
while (-1 != (len = inputStream.read(data))) {
responseBuffer.append(new String(data, 0, len, "UTF-8"));
size += len;
}
serverResponse = responseBuffer.toString();
return serverResponse;
}
}