我从android连接到restful web服务。我需要使用httpPost 添加信息。但这需要很长时间才能得到例外。 java.net.SocketException:操作超时
另一方面我可以制作httpGet,我不会采取任何异常
代码在这里。
ip等于我的计算机的本地IP是网络。 ip = 10.80.20.20
HttpClient httpclient = new DefaultHttpClient();
String url="http://"+ip+"/projectt/source/applySurvey";
HttpPost httppost = new HttpPost(url);
String strSurveyId=new Long(surveyId).toString();
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("surveyId",strSurveyId));
nameValuePairs.add(new BasicNameValuePair("questionId", "4"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
} catch (ClientProtocolException e) {
e.printStackTrace();
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
e.getCause();
我的清单文件在这里
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobil.survey.project"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".categoryList"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".firmsList" android:label="@string/app_name"></activity>
<activity android:name=".surveiesList" android:label="@string/app_name"></activity>
<activity android:name=".survey" android:label="@string/app_name"></activity>
</application>
</manifest>
定义了哪个安静的Web服务web.xml就在这里
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>projectt</display-name>
<servlet>
<servlet-name>FormValidator</servlet-name>
<servlet-class>com.project.servlets.FormValidator</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormValidator</servlet-name>
<url-pattern>/FormValidator</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.project.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/source/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
和我连接的uri资源在这里
@Path("/applySurvey")
public class AppliedSurveyResource {
@GET
@Produces( MediaType.APPLICATION_JSON)
public List<WSCategory> getCategories() {
List<WSCategory> categories = new ArrayList<WSCategory>();
categories.addAll( CategoryProvider.instance.getModel() );
return categories;
}
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void newTodo(
@FormParam("surveyId") Long surveyId,
@FormParam("questionId") Long questionId,
@FormParam("cellId") String cellId,
@Context HttpServletResponse servletResponse
) throws IOException {
PRAppliedSurvey appliedSurvey=new PRAppliedSurvey();
appliedSurvey.setSurveyId(surveyId);
appliedSurvey.setQuestionId(questionId);
appliedSurvey.setCellId(cellId);
java.util.Date today = new java.util.Date();
System.out.println(new java.sql.Timestamp(today.getTime()));
appliedSurvey.setApplyDt(new java.sql.Timestamp(today.getTime()));
try {
appliedSurvey.store();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*Todo todo = new Todo(id,summary);
if (description!=null){
todo.setDescription(description);
}
TodoDao.instance.getModel().put(id, todo);
URI uri = uriInfo.getAbsolutePathBuilder().path(id).build();
Response.created(uri).build();
servletResponse.sendRedirect("../create_todo.html");
*/
}
}
}
答案 0 :(得分:0)
尝试使用AsyncTask在不同的线程中运行它。 UI线程在引发超时之前具有相当低的阈值。这是为了防止UI对用户造成冻结。
http://developer.android.com/reference/android/os/AsyncTask.html