您可以使用以下所有方法在android中调用Web服务
public String postData(String result, JSONObject obj) {
// Create a new HttpClient and Post Header
String InsertTransactionResult = null;
HttpClient httpclient = new DefaultHttpClient();
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 1000);
HttpConnectionParams.setSoTimeout(myParams, 1000);
try {
HttpPost httppost = new HttpPost(result.toString());
httppost.setHeader("Content-type", "application/json");
StringEntity se = new StringEntity(obj.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
InsertTransactionResult = EntityUtils
.toString(response.getEntity());
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return InsertTransactionResult;
}
public String putData(String result, JSONObject obj) {
// Create a new HttpClient and Put Header
String UpdateTransactionResult = null;
HttpClient httpclient = new DefaultHttpClient();
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
try {
HttpPut httpPut = new HttpPut(result.toString());
httpPut.setHeader("Content-type", "application/json");
StringEntity se = new StringEntity(obj.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httpPut.setEntity(se);
HttpResponse response = httpclient.execute(httpPut);
UpdateTransactionResult = EntityUtils
.toString(response.getEntity());
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return UpdateTransactionResult;
}
public String deleteRecord(String result) {
// Create a new HttpClient and Get Header
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpDelete httpDelete = new HttpDelete(result.toString());
try {
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 0);
HttpResponse response = client.execute(httpDelete);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
System.out.println(response.toString());
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(LoginActivity.class.toString(), "Failed to Authenticate");
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
System.out.println(e);
}
return builder.toString();
}
public String getMethod(String result) {
// Create a new HttpClient and Get Header
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(result.toString());
try {
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 0);
HttpConnectionParams.setSoTimeout(myParams, 0);
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
System.out.println(response.toString());
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(LoginActivity.class.toString(), "Failed to Authenticate");
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return builder.toString();
}