我是 Web 服务的新手。我想在 Android 中集成 Web 服务(REST)。谁能推荐一些好的教程。
问问题
11431 次
2 回答
4
什么后端?如果是 JAVA,那么您可以使用Jersey 将 REST 与 Java (JAX-RS) 结合使用。
在 Android 端,您可以使用这个简单的 RestClient来使用该 REST 服务。
对于 JSON <--> 双方(Android、Java 后端)的对象映射,您可以使用GSON。
于 2013-04-05T07:22:17.963 回答
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;
}
}
于 2013-04-05T07:14:19.657 回答