处理HTTP GET /删除Android使用REST [英] Handling HTTP Get/ Delete Android using REST
问题描述
我米实施的Android基于REST的HTTP服务器。该服务器响应GET,DELETE和POST请求。两款Android设备之间使用HTTP POST(我米使用服务,其中设备保持侦听端口和后下一个设备上,并继续这样跌下去的)。
我米测试GET和使用Mozilla海报删除。我要补充一个单独的插座/端口来处理一样吗?因为当我现在尝试,有时我得到超时错误或没有找到回应。但是,我能够看到的logcat窗口服务器响应。请帮帮我。
code处理GET请求:
如果(method.equals(GET))
{
如果(checkFileExisting())
{
读者的BufferedReader =新的BufferedReader(新的FileReader(新文件(getFilesDir()+文件分割符+script.json)));
串读;
StringBuilder的建设者=新的StringBuilder();
而((读= reader.readLine())!= NULL)
{
builder.append(读);
}
串JSONContents = builder.toString();
reader.close();
的JSONObject的JSONObject;
尝试{
JSONObject的=新的JSONObject(JSONContents);
字符串名称= jsonObject.getString(名称);
JSONObject的stateObject = jsonObject.getJSONObject(国家);
字符串stateValue = stateObject.getString(值);
如果(name.equals(目标))
{
HTT presponse响应=新BasicHtt presponse(HttpVersion.HTTP_1_1,200,OK);
response.setEntity(新StringEntity(国家是:+ stateValue));
conn.sendResponseHeader(响应);
conn.sendResponseEntity(响应);
}
其他
{
HTT presponse响应=新BasicHtt presponse(HttpVersion.HTTP_1_1,404,未找到);
response.setEntity(新StringEntity(请求的资源+目标+不能因失配发现!));
conn.sendResponseHeader(响应);
conn.sendResponseEntity(响应);
}
}赶上(JSONException E){
e.printStackTrace();
}
}
其他
{
HTT presponse响应=新BasicHtt presponse(HttpVersion.HTTP_1_1,404,未找到);
response.setEntity(新StringEntity(请求的资源+目标+找不到!));
conn.sendResponseHeader(响应);
conn.sendResponseEntity(响应);
}
}
链接的 http://www.integratingstuff.com/2011/10/24/adding-a-webserver-to-an-android-app/ 有一个很好的例子。我在code错过conn.close()。
I m implementing a REST based HTTP server in Android. The server responds for GET, DELETE and POST requests. Two android devices communicate using HTTP Post (I m using a service, where a device keeps listening on a port and post to next device and this keeps going on).
I m testing the GET and DELETE using Mozilla Poster. Should I add a separate socket/port to handle the same? Because when I try now, sometimes I get timeout error or no response found. However, I am able to see server response in Logcat window. Please help me. Code to handle GET request:
if(method.equals("GET"))
{
if(checkFileExisting())
{
BufferedReader reader = new BufferedReader(new FileReader(new File(getFilesDir()+File.separator+"script.json")));
String read;
StringBuilder builder = new StringBuilder("");
while((read = reader.readLine()) != null)
{
builder.append(read);
}
String JSONContents = builder.toString();
reader.close();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(JSONContents);
String name = jsonObject.getString("name");
JSONObject stateObject = jsonObject.getJSONObject("state");
String stateValue = stateObject.getString("value");
if(name.equals(target))
{
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.setEntity(new StringEntity("State is:" + stateValue));
conn.sendResponseHeader(response);
conn.sendResponseEntity(response);
}
else
{
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 404, "Not Found");
response.setEntity(new StringEntity("The requested resource " + target + " could not be found due to mismatch!!"));
conn.sendResponseHeader(response);
conn.sendResponseEntity(response);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
else
{
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 404, "Not Found");
response.setEntity(new StringEntity("The requested resource " + target + " could not be found!!"));
conn.sendResponseHeader(response);
conn.sendResponseEntity(response);
}
}
The link http://www.integratingstuff.com/2011/10/24/adding-a-webserver-to-an-android-app/ has a very good example. I missed conn.close() in my code.
这篇关于处理HTTP GET /删除Android使用REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!