Within my Android application I want to connect to a PHP file on my server/web host. Currently I cannot POST data to the PHP file, I think I am passing the URL in an incorrect format.
Using a Google URL as an example, would this be correct in order to establish the path to my PHP file?
URL url = new URL("http://74.125.224.72/myFile.php");
It seems that you have an extra space in your URL
URL url = new URL("http://74.125.224.72 /myFile.php");
must be
URL url = new URL("http://74.125.224.72/myFile.php");
EDIT You can also use the android Uri class and build it using the Uri.Builder
Uri uri = new Uri.Builder()
.scheme("http")
.authority("74.125.224.72")
.appendPath("myFile.php")
.build();
Related
I have integrated PayUMoney in my app using https://github.com/payu-intrepos/Android-SDK-Sample-App but there is need to add some URL in MainActivity like
//TODO Below url is just for testing purpose, merchant needs to replace this with their server side hash generation url
URL url = new URL("https://payu.herokuapp.com/get_hash");
and also
//TODO Deploy a file on your server for storing cardToken and merchantHash nad replace below url with your server side file url.
URL url = new URL("https://payu.herokuapp.com/store_merchant_hash");
in storeMerchantHash Function
//TODO Replace below url with your server side file url.
URL url = new URL("https://payu.herokuapp.com/get_merchant_hashes");
in fetchMerchantHashes function.
These type number of url, How i can get it?
I am not getting how to get this URL, kindly guide me.
I am trying to create a URL but it seems that when I create the URL it isn't created with the full path.
Creating the URL
url = new URL("https://api.plivo.com/v1/Account/" + authID + "/Message/");
When I get the URL path
System.out.println(url.getPath());
The result is: I/System.out: /v1/Account/XXXXXXXXXXXXXXXX/Message/
Does anyone know how can I can solve this?
This is just printing out the "path" part of the URL (after the web address)
Try this to show what is happening, I think there is also a toString in url that will allow you to print the entire thing with just url
URL url = new URL("http://google.com/example");
System.out.println(url.getHost());
System.out.println(url.getPath());
System.out.println(url.getHost() + url.getPath());
This outputs
google.com
/example
google.com/example
Also see the reference
https://docs.oracle.com/javase/7/docs/api/java/net/URL.html#getPath()
SOLVED
Taking just the url returns me the full path
System.out.println(url);
WebView does not take url if they are bad formated ??
For example good url format http://www.youtube.com/embed/KeSzOIUJ4xY?rel=0 and
bad url format //player.vimeo.com/video/142545652?badge=0
Is there a way to accept the following bad url as browsers do ??
No android web view didnt take a bad formatted url..
Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority("www."+YOUR_STRING+".com")
.appendPath("turtles")
.appendPath("types")
.appendQueryParameter("type", "1")
.appendQueryParameter("sort", "relevance")
.fragment("section-name");
String myUrl = builder.build().toString();
But you can play with above code.
I use google-api-client for android. I try to do multipart POST request with text data and image file. Code snippet for creating request is below:
InputStream stream = new FileInputStream(fileToSend);
InputStreamContent photoContent = new InputStreamContent("image/jpeg", stream);
MultipartRelatedContent multiContent =
new MultipartRelatedContent(content, photoContent);
HttpRequest request = getRequestFactory().buildPostRequest(googleUrl, multiContent);
content is key-value text content. As a result I get error 500.
What I'm doing wrong?
There is a guide here about how to do media upload with the google-api-java-client here:
https://code.google.com/p/google-api-java-client/wiki/MediaUpload
That said, I don't anything necessarily wrong with your code either. It is possible that the googleUrl is incorrect, or that content is not properly formatted. You might want to try adding a URL query parameter uploadType=multipart to specify that you are using multipart as the protocol.
In my browser, or in iOS, when I try to get the contents of a URL with encoded http authentication information in the form
http://myUser:myPassword#www.example.com/secure/area/index.html
It just works. I'm getting URLs from a web service, and I'd like to avoid trying to parse them up for their HTTP auth info if I can help it. Is there a way to do something similar in Android without actually parsing the URLs? Alternatively, what is the best way to go about that?
UPDATE:
I find that when I try to set the authentication information in an Authorization header, I get a very strange FileNotFoundException.
Here's the code I'm using:
URL url = new URL(urlString);
URLConnection connection;
String authority = url.getAuthority();
if (authority.contains("#")) {
String userPasswordString = authority.split("#")[0];
url = new URL(urlString.replace(userPasswordString + "#", ""));
connection = url.openConnection();
String encoded = new String(Base64.encode(userPasswordString.getBytes(), Base64.DEFAULT), "UTF-8");
connection.setRequestProperty("Authorization", "Basic " + encoded);
} else {
connection = url.openConnection();
}
InputStream responseStream = connection.getInputStream();
All the info seems to check out, I've verified the url is correct, the base64 string is correct, and the file is certainly on the server--I have no trouble at all opening it with Firefox, and Firebug shows all the right headers, matching what I've sent as far as I can tell. What I get though is the following error (url host changed to protect the innocent):
java.io.FileNotFoundException: http://a1b.example.com/grid/uploads/profile/avatar/user1/custom-avatar.jpg
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1061)
Any idea what this is all about?
I looked into using HttpClient, but saw that in Issue 16041 it is recommended that we prefer URLConnection.
That looks like your browser is applying some extra rules to parsing the URL. In Android you can use HTTP Client's authentication mechanism such as BASIC and DIGEST to do the same things. Which one you choose is dependent on the server you are trying to authenticate against.
Here is a good page to get you started.
Unfortunately, on Android you can't pass the user info (username/password) in that format to either java.net.URL or HttpClient and have it work like in a browser.
I'd recommend using URI (see http://download.oracle.com/javase/1.5.0/docs/api/index.html?java/net/URI.html) to do this: pass your URL to the URI constructor that takes a String and then you can extract the user info (using getUserInfo()). You can then either use HttpClient's authorization classes (see http://developer.android.com/reference/org/apache/http/auth/package-summary.html) or build the basic auth header yourself (an example is given at http://www.avajava.com/tutorials/lessons/how-do-i-connect-to-a-url-using-basic-authentication.html).