Why is my HTTPost in android gives me null on JSP page? -
i building app tracks user latitude , longitude. latitude , longitude has posted server.
i build service gives me users current location.
this code:
myservice.java
package com.example.rama.locationtracking; /** * created rama on 4/10/2016. */ public class myservice extends service { private static final string tag = "sri"; private locationmanager mlocationmanager = null; private static final int location_interval = 1000; private static final float location_distance = 0; private double longitude,latitude; private static string url ="http://49.206.9.182:8080/ezomart/latlong.jsp"; private string latitude_str; private string longitude_str; private class locationlistener implements android.location.locationlistener { location mlastlocation; public locationlistener(string provider) { log.e(tag, "locationlistener " + provider); mlastlocation = new location(provider); } @override public void onlocationchanged(location location) { log.e(tag, "onlocationchanged: " + location); longitude = location.getlongitude(); latitude = location.getlatitude(); posttowebserver(longitude,latitude); mlastlocation.set(location); } @override public void onproviderdisabled(string provider) { log.e(tag, "onproviderdisabled: " + provider); } @override public void onproviderenabled(string provider) { log.e(tag, "onproviderenabled: " + provider); } @override public void onstatuschanged(string provider, int status, bundle extras) { log.e(tag, "onstatuschanged: " + provider); } } private void posttowebserver(double longitude, double latitude) { latitude_str = string.valueof(latitude); longitude_str = string.valueof(longitude); log.e(tag,latitude_str); log.e(tag, longitude_str); new thread(new runnable() { @override public void run() { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://49.206.9.182:8080/ezomart/latlong.jsp"); //post data list<namevaluepair> namevaluepair = new arraylist<namevaluepair>(2); namevaluepair.add(new basicnamevaluepair("username", latitude_str)); namevaluepair.add(new basicnamevaluepair("password", longitude_str)); //encoding post data try { httppost.setentity(new urlencodedformentity(namevaluepair)); } catch (unsupportedencodingexception e) { // log exception e.printstacktrace(); } //making post request. try { httpresponse response = httpclient.execute(httppost); // write response log string responsebody = entityutils.tostring(response.getentity()); // log.d("http post response:", responsebody); } catch (clientprotocolexception e) { // log exception e.printstacktrace(); } catch (ioexception e) { // log exception e.printstacktrace(); } } }).start(); } locationlistener[] mlocationlisteners = new locationlistener[]{ new locationlistener(locationmanager.gps_provider), new locationlistener(locationmanager.network_provider) }; @override public ibinder onbind(intent arg0) { return null; } @override public int onstartcommand(intent intent, int flags, int startid) { log.e(tag, "onstartcommand"); super.onstartcommand(intent, flags, startid); return start_sticky; } @override public void oncreate() { log.e(tag, "oncreate"); initializelocationmanager(); // makepostrequestonnewthread(); try { mlocationmanager.requestlocationupdates( locationmanager.network_provider, location_interval, location_distance, mlocationlisteners[1]); } catch (java.lang.securityexception ex) { log.i(tag, "fail request location update, ignore", ex); } catch (illegalargumentexception ex) { log.d(tag, "network provider not exist, " + ex.getmessage()); } try { mlocationmanager.requestlocationupdates( locationmanager.gps_provider, location_interval, location_distance, mlocationlisteners[0]); } catch (java.lang.securityexception ex) { log.i(tag, "fail request location update, ignore", ex); } catch (illegalargumentexception ex) { log.d(tag, "gps provider not exist " + ex.getmessage()); } } @override public void ondestroy() { log.e(tag, "ondestroy"); super.ondestroy(); if (mlocationmanager != null) { (int = 0; < mlocationlisteners.length; i++) { try { if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { // todo: consider calling // activitycompat#requestpermissions // here request missing permissions, , overriding // public void onrequestpermissionsresult(int requestcode, string[] permissions, // int[] grantresults) // handle case user grants permission. see documentation // activitycompat#requestpermissions more details. return; } mlocationmanager.removeupdates(mlocationlisteners[i]); } catch (exception ex) { log.i(tag, "fail remove location listners, ignore", ex); } } } } private void initializelocationmanager() { log.e(tag, "initializelocationmanager"); if (mlocationmanager == null) { mlocationmanager = (locationmanager) getapplicationcontext().getsystemservice(context.location_service); } } }
mainactivity.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); startservice(new intent(this, myservice.class)); }
jsp code
<%@page contenttype="text/html" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>jsp page</title> </head> <body> <% string lat=request.getparameter("username"); string longg=request.getparameter("password"); %> <h1>hello world!</h1> <%=lat%>, <%=longg%> </body> </html>
Comments
Post a Comment