I have developed android base application and other jsf base web application.
我的项目是我必须将一些价值从 androide 应用程序传递给 jsf 基础 Web 应用程序。
我正在使用 httpclient 方法。
我发送价值的代码是。
public void loc(double lat,double lon)
{
String a=String.valueOf(lat);
System.out.println("Value of latitude " + lat);
Log.d(a, "dfdfdfdfdfdfdfdfdfdfdfdfdddddddddddddd");
System.out.println("Value of longitude " + lon);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:8080/q/faces/getLocation.jsp");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("lat", "lat"));
nameValuePairs
.add(new BasicNameValuePair("lon", "lon"));
//nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
if (line.startsWith("Auth=")) {
String key = line.substring(5);
// Do something with the key
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
这段代码也可以工作。
我的问题是我如何从 jsf 代码中检索这样的值。
我用过的技术,
- jsf
- 安卓
我已经编写代码来获得价值
import org.apache.http.HttpException;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class getData {
private static String url = "http://localhost:8080/q/faces/getdata.jsp";
public String mainm() {
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
// String result = method.getResponseBodyAsString();
// this is the document returned by the response
// System.out.println(result);
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
}
catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
return "";
}
}
谢谢