httpPost takes to long and get this exception : java.net.SocketException: The operation timed out
i connect to restful web service from android . I need to use httpPost to add information. But it takes to long and i get exception. java.net.SocketException: The operation timed out
on the other hand i can make httpGet , i don't take any exception
the code is here.
ip equals to my computer's ip at local are network. 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();
My manifest file is here
<?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>
Which restful web service is defined web.xml is here
<?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/*&l开发者_如何学Pythont;/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>
and the uri resource I connected is here
@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");
*/
}
}
}
Try running it in a different thread using AsyncTask. The UI thread has a pretty low threshold before it throws a timeout. This is to keep the UI from freezing for the user.
http://developer.android.com/reference/android/os/AsyncTask.html
精彩评论