Ganesh Gadekar
Ganesh Gadekar

Reputation: 21

to send value from android gps app to jsf web application

  I have developed android base application and other jsf base web application.  

My project is that I have to pass some value from androide application to jsf base web application.
I am using httpclient method.
My code to send value is.

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();
}
}  

so will this code work.

and my question is that how i retrive such value from jsf code.

Technology i have used,

I have write code to get value

    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 "";
  }
}

thanks

Upvotes: 0

Views: 463

Answers (1)

divaNilisha
divaNilisha

Reputation: 693

nameValuePairs.add(new BasicNameValuePair("lat", "lat"));
    nameValuePairs
            .add(new BasicNameValuePair("lon", "lon"));

what is "lat" and lon here ? If they are any variables remove inverted comas. like this :

nameValuePairs.add(new BasicNameValuePair("lat", lat));
        nameValuePairs
                .add(new BasicNameValuePair("lon", lon));

It would be better if you tell me what are lat/lon . Right click if you accept this answer

Upvotes: 2

Related Questions