I'm new to android programming. I'm looking for a simple way to send pictures to Picasa, I looked at a lot of projects on it. I'm just looking to send a JPEG or PNG button I click, sends and displays a message that it is OK.
I know that is required a Google API and client authentication, but a lot of people show the same Intention sent.
Please help (sorry for the english: P)
I found this:
http://code.google.com/p/google-api-java-client/source/browse?repo=samples#hg/picasa-android-sample
Someone knows how to use it? But from the basics, I'm lost.
The only existing code in online for uploading photos to Picasa is this one..
Picasa Photo Uploader
Try with this one whether it can meet your requirements.If it does,then engage it with a button click event and display message on notification.finished() event to ensure that the file has been uploaded.
Quite an old post, but just for future references, I was successful in directly using http post to upload my image to Picasa. Their own Java API keeps returning errors.
I've written about this method in detail here:
File image = new File("/path/to/image.jpg");
byte[] imageContent = null;
try {
imageContent = Files.toByteArray(image);
} catch (Exception e) {
// do something
}
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("https://picasaweb.google.com/data/feed/api/user/default/albumid/default");
httpPost.addHeader("Authorization", "Bearer " + mAccessToken);
httpPost.addHeader("Content-Type", "image/jpeg");
httpPost.setEntity(new ByteArrayEntity(imageContent));
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
// log the response
logd(EntityUtils.toString(httpResponse.getEntity()));
} catch (IOException e){
// do something
}
This method uses Apache's HttpClient. If your Android version does not support it, you can still include this line in your Gradle file to compile it:
compile 'cz.msebera.android:httpclient:4.4.1.1'
Related
Im new to Android development but Im trying to do an application for Opencart to allow users to enter in their own store to administrate it.
Lets go to the point. In order to get the information from the store i created a page where all the information is presented in XML, so the idea is that the user login, and then redirects to this page and with the http response, parse the xml and voilá!.
I have already the xml parser, but Im having some difficulties with the http connection. Let me explain a little bit more:
Basically, to log into any store, you need to go to www.example.com/admin (I will be using my testing online address to see if someone is able to help me), in this case http://www.onlineshop.davisanchezplaza.com/admin . Once we arrive to the page we arrive to the login system. The login system uses post to send the username: admin and password:admin and redirects to http://onlineshop.davidsanchezplaza.com/admin/index.php?route=common/login and once it verify your identity, it gives you a Token (here I start having some problems). http://onlineshop.davidsanchezplaza.com/admin/index.php?route=common/home&token=8e64583e003a4eedf54aa07cb3e48150 . Well, till here, im very okay, and actually developed an app that can do till here, actually i can "hardcode" read the token from the http response it sends me (what is actually not very good).
Here comes my first question: HOW TO GET FROM THE HTTPresponse the token value? (by now, as I said, I can only get the token by reading all the response, and if we find the string token=, take what comes next ... not good).
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpPost httpPost = new HttpPost("http://onlineshop.davidsanchezplaza.com/admin/index.php?route=common/login");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "admin"));
nameValuePairs.add(new BasicNameValuePair("password", "admin"));
try{
Log.d(DEBUG_TAG, "Try ");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 8096);
Log.d(DEBUG_TAG, "br :" + br);
String line;
while ((line = br.readLine()) != null) {
Log.d(DEBUG_TAG, "br :" + line);
if(line.contains("token=")){
int index = line.indexOf("token=");
String aux = line.substring(index + "token=".length(), index + 32 + "token=".length());
token = aux; //Yes, I know, its not the best way.
break;
}
}
} catch (IOException e) {
Log.d(DEBUG_TAG, "Finally");
}
Second question, (and more important), now having the token (in the example 8e64583e003a4eedf54aa07cb3e48150), I need to go to the route android/home where is the xml information generated. (http://onlineshop.davidsanchezplaza.com/admin/index.php?route=android/home2&token=8e64583e003a4eedf54aa07cb3e48150). As I was reading, in httpget, we can either set the parameters, or directly send the url with the parameters already inside the url. Is in this step where it stops. Maybe is the internet connexion in China, maybe (most sure) im doing something wrong. Sometimes it just come the timeout connexion, others it just send me back to the login page.
Here is the code how i do (edit: I was a noob, and didnt create the httpclient to receive the answer, sorry!):
String s = "http://onlineshop.davidsanchezplaza.com/admin/index.php?route=common/home&token=";
String tot = s.concat(token);
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpGet httpget = new HttpGet(tot);
try{
Log.d(DEBUG_TAG, "Try ");
HttpResponse response = httpClient.execute(httpget);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 8096);
Log.d(DEBUG_TAG, "br :" + br);
} catch (IOException e) {
Log.d(DEBUG_TAG, "Finally");
}
I dont need someone to tell me how to do it, just need a little guidance to solve the issue, I really appreciate any comment or help you can offer, or extra documentation :).
As a bonus, if someone can give me further details about how can I test the http get, I will appreciate, I only know how to write it in the web browser, and works fine.
It's a while since I last did something for Android, but here is my advice:
for the login purpose from Android application into the OpenCart administration I recommend creating a new mobile login page, e.g. instead of accessing http://yourstore.com/admin/ which redirects You to http://.../admin/index.php?route=common/login create Your own action e.g. androidLogin() within this controller (admin/controller/common/login.php and You will access it directly via http://yourstore.com/admin/index.php?route=common/login/androidLogin. Why special action? Because the default login action redirects the user (using normal browser) to the home while setting the security token into the URL within the query string part. In Your own action You won't redirect but respond with XML containing this security token so that You can easily extract that token using Your XML parser.
I cannot address second problem exactly but from what I remember I was passing a query string in different way (now I cannot find any similar solution on the internet).
Here is my 5 cents for the second question :
After playing a bit with the browser I realized :
Set Cookies
Your request to ...?route=android/home2&token= seems to be rejected if you are missing cookies. That is, you probably need to extract cookies from first server response and set them for further requests either manually (via conn.setRequestProperty("Cookie", cookie); or using Android CookieManager
User agent
Some server may reject your request just because you are missing "User-Agent" property in request header. To be safe, you could set it to something like conn.setRequestProperty("User-Agent", "Mozilla/5.0");
Extra note - I suggest that you also handle redirects correctly, as for example when you POST your admin/admin credentials you get 302 response and redirected to ...?route=common/home page
Also, you don't need to set conn.setDoInput(true) for UrlConnection while doing GET request.
Hope that helps.
I don't see any catch statement for the try in the second question, this catch may have the info you need to know what's going on.
For the first question try to convert InputStreamReader to a String, and use the String for a
url constructor, with the url (or uri i'm not sure right now, and can't test it) object try .getQueryParameter("parameter").
For your second question when i tried to login using the token that you have provided, the web page replied with invalid token. Can you login with the token that you have provided? If not, try to get a new token. Maybe the token have expired.
I am a beginner on Android app development. I want to confirm if my approach is correct and as per best practices in Android world.
I have an android application that needs textual data (no graphic, video). The data comes from REST based web service as JSON string. I consume this web service using HttpGet object, parse json string using JSONArray and display data.
All this happens in a button click.
My questions are:
Is there a better (or android-style) approach to do the same?
What is the preferred approach to retrieve and post graphic contents to REST based web service?
Any help is most appreciated.
Thanks
Please find my inline commnets,
All this happens in a button click.
My questions are:
Is there a better (or android-style) approach to do the same?
Ideal approach to trigger the Webservice calls in Async task or in a service, so your UI thread will not be blocked till HTTP fires and get the response.
What is the preferred approach to retrieve and post graphic contents to REST based web service?
The graphics content will be usually base64 when you try to retrieve it from the backend.
Refer this example : http://androidtrainningcenter.blogspot.in/2012/03/how-to-convert-string-to-bitmap-and.html
for the posting the graphics to the server
I'm going to assume that you know the path and filename of the image that you want to upload. Add this string to your NameValuePair using image as the key-name.
Sending images can be done using the HttpComponents libraries. Download the latest HttpClient (currently 4.0.1) binary with dependencies package and copy apache-mime4j-0.6.jar and httpmime-4.0.1.jar to your project and add them to your Java build path.
You will need to add the following imports to your class.
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
Now you can create a MultipartEntity to attach an image to your POST request. The following code shows an example of how to do this:
public void post(String url, List nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int index=0; index < nameValuePairs.size(); index++) {
if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
// If the key equals to "image", we use FileBody to transfer the data
entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
} else {
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
}
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
} catch (IOException e) {
e.printStackTrace();
}
}
Hope this helps!
Hey refer this link which will train you better in parsing json data as you required in Android App. Loading the graphic required some lazy loading mechanism which you can refer it from here.
So ultimately I'm trying to upload images that I want Google to OCR. Then I want to be able to get the results of the OCR back to my Android app. I have my images uploading properly. I can loop through all the files in my google drive and I see that there are export links available, one of which is "text/plain". If I use one of these urls in a browser, it downloads the text. Is this the way I should be trying to access it?
I've tried to use the url I get from calling getExportLinks method on the file returned by the insert method
File file = drive.files().insert(body, mediaContent).setOcr(true).execute();
String imageAsTextUrl = getExportLinks.get("text/plain")
I end up getting HTML back that appears to be the Google Drive home page. To get the exported url document, I used google drive instance so it should have properly authenticated like the insert method I would think.
DriveRequest request = new DriveRequest(drive, HttpMethod.GET, imageAsTextUrl, null);
Has anyone tried to do this before? What am I doing wrong?
Well I answered my own question yet again, sort of. Basically since this seems to be a web url and not an API call I can make, then it's not responding with a 401 if it's unauthenticated. So basically the response I was getting is the HTML for the login page. Apparently using DriveRequest does not automatically handle authentication like I thought it would. So I have it working by adding authentication manually to an HttpClient GET call.
But is there a way to do what I'm trying to do with the actual API? So I can deal with response codes?
Here's what I did to download the text/plain representation of the file. Here's a caveat: given that the image I was uploading was taken on a cell phone camera using the default camera app, the default dpi and/or jpeg compression caused the OCR to not work very well. Anyway, here's the code I used. Just basic HttpClient stuff
String imageAsTextUrl = file.getExportLinks().get("text/plain");
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(imageAsTextUrl);
get.setHeader("Authorization", "Bearer " + token);
HttpResponse response = client.execute(get);
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String str;
while ((str = in.readLine()) != null) {
sb.append(str);
}
}
finally {
if (in != null) {
in.close();
}
}
// Send data to new Intent to display:
Intent intent = new Intent(UploadImageService.this, VerifyTextActivity.class);
intent.putExtra("ocrText", sb.toString());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I'm developing an app for which I need to be able to upload pics to picasa open album.
Ive gone through many threads,forums... tried a couple of methods using http post but none seems to work.
Has anyone did it before? If so can you share a sample code.I just need to do the basic upload and dload of pics from picasa.
The following question looks to cover some of this.
Picasa access in android: PicasaUploadActivity
This thread also has information. http://www.mail-archive.com/android-developers#googlegroups.com/msg43707.html
It looks straight forward to fire off the intent to use the standard picasa uploader. I will try putting this in my app later today as I want this function.
Doing it yourself looks to be possible but clearly more complex documentation looks to be http://code.google.com/apis/picasaweb/docs/2.0/developers_guide_protocol.html
OK I have got it working with the following code in my app. This brings up the picasa uploader.
Intent temp = new Intent(Intent.ACTION_SEND);
temp.setType("image/png");
temp.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
temp.putExtra(Intent.EXTRA_STREAM, fileUri);
temp.setComponent(new ComponentName(
"com.google.android.apps.uploader",
"com.google.android.apps.uploader.clients.picasa.PicasaSettingsActivity"));
try {
startActivity(temp);
} catch (android.content.ActivityNotFoundException ex) {
Log.v(TAG, "Picasa failed");
}
In practice I am going to take the set component bit out which lets the use choose where and how to send which is what I want.
The above answer was for Picasa API v2, which is now deprecated. I was not able to successfully use the Java API for Picasa API v3, but I figured out a way to upload images to Picasa using http post. I've written about this method here:
File image = new File("/path/to/image.jpg");
byte[] imageContent = null;
try {
imageContent = Files.toByteArray(image);
} catch (Exception e) {
// do something
}
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("https://picasaweb.google.com/data/feed/api/user/default/albumid/default");
httpPost.addHeader("Authorization", "Bearer " + mAccessToken);
httpPost.addHeader("Content-Type", "image/jpeg");
httpPost.setEntity(new ByteArrayEntity(imageContent));
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
// log the response
logd(EntityUtils.toString(httpResponse.getEntity()));
} catch (IOException e){
// do something
}
This method uses Apache's HttpClient. If your Android version does not support it, you can still include this line in your Gradle file to compile it:
compile 'cz.msebera.android:httpclient:4.4.1.1'
I'm taking a mobile development class, which focuses on Android, and for my term project I thought it would be cool if I made a little application that returns a list of cancer-related events and fundraisers. Basically what I have to do is programmatically fill in a webform given criteria that is input from my application, and parse the returned results to give a list of events, because for some reason the American Cancer Society doesn't keep a public list of all events. This is my first real experience with android, and I have almost zero experience with network programming. If I really wanted to, I could just change the URL I go to based on the paramaters given to me, because the ACS event search URLs all look almost exactly the same, but I want to do it "right". I looked at this post and this one for guidance, which led me to the MultipartEntity. They've been very helpful, but I really am not sure what to do next. Code is below:
//Base case, creates entity based on Entered ZIP Code
public void sendRequest()
{
EditText MyEditText = (EditText)findViewById(R.id.zip_edit_text);
String ZIP = MyEditText.getText().toString();
HttpClient defaultClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.cancer.org/Involved/Participate/app/event-search");
try{
MultipartEntity entity = new MultipartEntity();
entity.addPart("ZIP",new StringBody(ZIP));
httppost.setEntity(entity);
HttpResponse response = defaultClient.execute(httppost);
HttpEntity result = response.getEntity();
InputStream stream = result.getContent();
String s = new Scanner(stream).useDelimiter("\\A").next();
Intent intent = new Intent(HomeScreen.this, ListResults.class);
startActivity(intent);
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setMessage(s);
dialog.show();
}catch (ClientProtocolException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
It's pretty bare-bones right now, as you can see. The AlertDialog is used just to see what the HttpResponse looks like, and it seems like it does the POST correctly, and the ZIP code ends up in the right text field, but it doesn't actually "click" the search button. Personally, I think either:
1.) My HttpPost object's URL was incorrect
2.) I used POST instead of GET, or i should POST then GET
I really have tried to work this out myself, and have searched StackOverflow, but I've really come to a rough patch, and as I said before, my network programming experience is near nonexistent. Any help would be appreciated.
I would suggest that you do a printout the URL that was sent through your multipart method, do a search via the web browser, and see if both URL matches. If the URL doesn't match, it means that there's something wrong while setting your entity, etc.