Calling an Object inside uriparse while opening google map using a button - android

Here am using a button to show a gps from one loaction to another...
I am having a problem {Uri.parse("http://maps.google.com/maps?saddr=10.0515097,76.4888416&daddr="+obj.latitude,+obj.longitude));}
while using this...
I am using a web service so latitude and longitude is saved as objects...
I got the gps location without a coma in google map between
+obj.latitude+obj.longitude{Uri.parse("http://maps.google.com/maps?saddr=10.0515097,76.4888416&daddr="+obj.latitude+obj.longitude));}
What should I do to add the comma in between them to track it
Button btn_driver = (Button) v.findViewById(R.id.btn_drivermap);
btn_driver.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("SECOND", "doInBackground()");
HttpEntity resEntity;
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Utils.WebserviceUrl + "get_drivers.php?");
MultipartEntity reqEntity = new MultipartEntity();
try {
reqEntity.addPart("user_id", new StringBody(Utils.user_id));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
reqEntity.addPart("latitude", new StringBody(String.valueOf(Utils.geo_latitude)));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reqEntity.addPart("longitude", new StringBody(String.valueOf(Utils.geo_longitude)));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=10.0515097,76.4888416&daddr="+obj.latitude,+obj.longitude));
startActivity(intent);
}});

Just added a plus(+) to it
Uri.parse("http://maps.google.com/maps?saddr=10.0515097,76.4888416&daddr="+obj.latitude+","+obj.longitude));

Related

Google places adding event invalid request

I'm trying to add an event in google places and the JSON returned has an invalid request in status and this endpoint has been removed in error_message. This morning it has worked correctly but now it doesn't work anymore.
I tried to use HTTP in the request to see if it returns the request denied but this doesn't do it, it always returns invalid request.
Here is my code:
private final String EVENT_URL =
"https://maps.googleapis.com/maps/api/place/event/add/json?";
private JSONObject uploadEvent() {
JSONObject jsonobj = new JSONObject();
StringEntity sEntity = null;
try {
jsonobj.put("duration", duration);
jsonobj.put("reference", reference);
jsonobj.put("summary", description);
if(url.length() > 0){
jsonobj.put("url", url);
}
sEntity = new StringEntity(jsonobj.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppostreq = new HttpPost(getUrlEvent());
httppostreq.setEntity(sEntity);
String responseText;
JSONObject JSON = null;
try {
HttpResponse httpresponse = httpclient.execute(httppostreq);
responseText = EntityUtils.toString(httpresponse.getEntity());
JSON = new JSONObject(responseText);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
private String getUrlEvent(){
//genero la solicitud como la debajo descrita
StringBuilder urlString = new StringBuilder(EVENT_URL);
urlString.append("&sensor=false&key=" + API_KEY);
return urlString.toString();
}
Can anyone helps me please?
Try
sEntity.setContentEncoding("UTF-8");
sEntity.setContentType("application/json");
httppostreq.addHeader("Content-Type", "application/json");
To quote the documentation:
Note: Events and place bumps were deprecated on September 3, 2014. The deprecation period has now ended. As of February 10th, 2015, the
API no longer accepts event or bump actions, and no longer returns
events in Place Search or Place Details responses.

HTTP Post to Php isn't working

The code isn't working. Please help. The item gets added to the database except for the quantity. It's always zero. Why is that?
String value;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.creamy);
EditText et = (EditText) findViewById(R.id.CreamyEdit);
value = et.getText().toString();
Button b = (Button) findViewById(R.id.buttonCreamy);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(Creamy.this, "Item Submitted", Toast.LENGTH_LONG).show();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"http://10.0.2.2:8080/http/test.php");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("item", "Creamy Delight")); //reflected in db
pairs.add(new BasicNameValuePair("quantity", value)); //not reflected in db
try {
post.setEntity(new UrlEncodedFormEntity(pairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
HttpResponse response = client.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
});
Help please!
Move the line value = et.getText().toString(); into the onClick method. If you call et.getText in onCreate it always returns an empty string, because that's all that's inside the EditText at the time of onCreate.

upload many images from android Sd to PHp in samsung is9023

iused:
private void postFile() {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(ConstValue.SEARCH_BY_PICTER);
File file = new File(currentPath);
ContentBody cbFile = new FileBody(file, "image/jpeg");
MultipartEntity mpEntity = new MultipartEntity();
try {
mpEntity.addPart("fileToUpload", cbFile);
mpEntity.addPart("sys", new StringBody("android"));
mpEntity.addPart("ver", new StringBody(ConstValue.VERSION_NAME));
mpEntity.addPart("cid", new StringBody(cid));
mpEntity.addPart("page", new StringBody("1"));
mpEntity.addPart("offset", new StringBody("80"));
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
httppost.setEntity(mpEntity);
// System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpEntity resEntity = response.getEntity();
// System.out.println(response.getStatusLine());
if (resEntity != null) {
try {
final String response_str = EntityUtils.toString(resEntity);
if (!response_str.contains("####")) {
ConstValue.goodByPiclist = GoodXMLUtil.parseResponse(response_str);
} else {
Toast.makeText(context, "sorry,server is busy", 2000).show();
}
// Log.d("log", response_str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (resEntity != null) {
try {
resEntity.consumeContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
httpclient.getConnectionManager().shutdown();
}
i used jar is httpmime-4.1.2.jar,the code is very well in android and other phone.but in samsung is9023 not run,if my code have mistake,if yes can you give me some advice or some good other method.very very thank you
edit: sorry i have solved the problem ,my code is right,and samsung is good,my mistake is i put image size is not valid
edit:i have meet new problem,in samsung only upload Pure color succeed as Pure white,Pure Black.other htc android phone any color ie very well.if samsung is9023 not trust httpmime-4.1.2.jar very well.because in php not receive any data if i used other pic in samsung
one possibilty can be that currentPath is hard coded like ../sdcard/ and as every device have diffrent external storage location so use Environment.getExternalDirectory() instead .

Android - HttpClient execute has a mind of its own - 1 second or 200 seconds

I'm trying to use HttpClient to connect to a php page that logs in and passes back a sessionid and then goes to a new page, using that sessionid and obtains a mySQL datafield associated with that sessionid.
On the first request, HttpClient can take 1.5 seconds, 6 seconds, or 2 minutes. If the first request was slow, subsequence requests seem to be faster, and visaversa.
The HttpClient request occurs when a Button view is clicked
Here's my code:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
name = (TextView)findViewById(R.id.name);
user = (EditText) findViewById(R.id.user);
pass = (EditText) findViewById(R.id.pass);
submit = (Button) findViewById(R.id.button1);
submit.setOnClickListener(this);
HttpParams params1 = new BasicHttpParams();
params1.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
client = new DefaultHttpClient(params1);
httpclient = new DefaultHttpClient();
// Create a local instance of cookie store
cookieStore = new BasicCookieStore();
// Create local HTTP context
}
public void onClick(View v) {
if (v.getId() == R.id.button1) {
//submit.setClickable(false);
String username = user.getText().toString();
String password = pass.getText().toString();
String targetURL = "<<<<LOGIN PHP URL>>>>";
post = new HttpPost(targetURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("params","params added");
try {
post.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Log.d("entity added","entityadded");
Log.d("preex","PRE EXECUTION");
localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//submit.setText("Logging In...");
new Thread(new Runnable(){
public void run(){
try {
Log.d("pre","pre execute");
response = client.execute(post,localContext);
Log.d("post","post execute");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
Log.d("post","FIANLLY");
try {
input = response.getEntity().getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("response: ",convertStreamToString(input));
getFullName(localContext);
}
}
}).start();}
}
private void getFullName(final HttpContext context){
Log.d("called","called");
String targetURL = "<<<<SESSION CHECKER PHP URL>>>>";
//client1 = new DefaultHttpClient();
post1 = new HttpPost(targetURL);
Log.d("","about to call runable....");
// submit.setText("Obtaining Full Name...");
try {
Log.d("pre","CALLING!");
response = client.execute(post1,context);
Log.d("post","called..");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
Log.d("post","FIANLLY");
try {
//submit.setText("Full Name Obtained!...");
input = response.getEntity().getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Log.d("response: ",convertStreamToString(input));
outputResponse(input);
}
}
private void outputResponse(final InputStream in) {
name.post(new Runnable(){
public void run(){
String fullname=convertStreamToString(in);
name.setText("Full Name is: "+fullname);
}
});
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Before I set Http version 1.1 it took double the time, but for my application, the speed cannot be unreliable. This is all on a pretty fast WiFi connections -- can you image Edge or 3G speeds??
So what can I optimize?
Thanks everyone:)
EDIT: I did a new test with: http://www.posttestserver.com/ and it happened pretty fast. The urls I'm using currently aren't my final server urls, they are for a different site on a different server -- shared hosting, so could it be that contacting my shared hosting site is just slow and will be compared to my .net server?
Thanks again !

Parameters not being passed into URL in Android

Ok, I have looked at multiple examples and my code seems to be correct, but for what ever reason the parameters are not being added to the URL. I'm trying to connect to the Last.Fm API and my code is as follows:
searchIT.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
//Create new HTTPClient and Post header
HttpPost API_ROOT = new HttpPost("http://ws.audioscrobbler.com/2.0/");
HttpClient httpclient = new DefaultHttpClient();
try
{
//Add Data
List<NameValuePair> nameValPairs = new ArrayList<NameValuePair>(4);
nameValPairs.add(new BasicNameValuePair("method", "artist.getevents")); //Get events for artist
nameValPairs.add(new BasicNameValuePair("artist", namesBox.getText().toString())); //Get artist name
nameValPairs.add(new BasicNameValuePair("autocorrect", "1")); //Turn on AutoCorrect
nameValPairs.add(new BasicNameValuePair("api_key", "xxxxxxxxxxxxxxxxxxxx")); //API Key - redacted for privacy
API_ROOT.setEntity(new UrlEncodedFormEntity(nameValPairs));
Log.i(TAG, "URL: " + API_ROOT.getURI().toString());
//Execute HTTP Post Request
HttpResponse response = httpclient.execute(API_ROOT);
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Am I just missing something here? In the log the url is shown as: http://ws.audioscrobbler.com/2.0/ so it seems to missing all the parameters I was trying to pass into it. Any ideas, tips, corrections or suggestions? Thanks!
From your onClick method start a new activity and pass all the parameters you want to as
HashMap<String, String> o = (HashMap<String, String>) searchList.getItemAtPosition(position);
Intent i = new Intent(SearchActivity.this, SearchDetails.class);
i.putExtra("method", o.get("method"));
i.putExtra("artist", o.get("artist"));
i.putExtra("autocorrect", o.get("autocorrect"));
i.putExtra("api_key", o.get("api_key"));
Then in your new activity: In the onCreate method
Intent myIntent = getIntent();
String method= myIntent.getStringExtra("method");
String artist= myIntent.getStringExtra("artist");
String autocorrect= myIntent.getStringExtra("autocorrect");
String api_key= myIntent.getStringExtra("api_key");
Now you'll get the params as you require and add them to your URL.
Hope it works

Categories

Resources