how to get result of CamFind API? - android

I am using CamFind API for image recognition in my app. I am following CamFind Tutorial
but stuck at how to get response from that API after posting data? How you got my question.

try this code
try {
HttpResponse<JsonNode> request = Unirest
.post("https://camfind.p.mashape.com/image_requests")
.header("X-Mashape-Authorization",
"A0MYOpCsdfasdgadfadafgdj7vsdfe")
.field("image_request[locale]", "en_US")
.field("image_request[image]",
new File("your Image path")).asJson();
String body = request.getBody().toString();
Log.v("body", body);
} catch (UnirestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
now the body string contain the json response String with a Token variable inside it
and to get the recognition name do the same again, but remove the fields and add the Token value after the url
so it will look like this:
https://camfind.p.mashape.com/image_requests/(Token Vlaue)
now the body string will contain the name value

Related

Android Studio Weird error with volley and response from volley

Am I missing something, I am trying to create a loginActivity and I created this code, and I have a log that logs the answer to the volley I created...But The if statement is not getting entered...Can someone see something I'm doing wrong? Sorry if its really dump( and yes I tried to change the response.equals check to be response.equals(" Password Accepted!");, this did not work either)
Another thing to note, yes I know my search in the log is different then the tag, it doesnt login, (which is what i want it to do)
Toast.makeText(getApplication(), response, Toast.LENGTH_LONG).show();
Log.e("Before Conversion",response);
String convertedResponse = null;
try {
convertedResponse = new String(response.getBytes("ISO-8859-1"), "UTF-8");
L.e("After Conversion",convertedResponse);
if (convertedResponse.equals("Password Accepted!")) {
showProgress(false);
Message msg = new Message();
msg.what = 1;
msg.obj = succcess ? 1 : 0;
handler.sendMessage(msg);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
06-29 15:24:04.990 16707-16707/E/Before Conversion: Password Accepted!
06-29 15:24:04.990 16707-16707/E/After Conversion: Password Accepted!
After suggestion to change logging...
06-29 15:34:32.339 16707-16707/ E/Got a response:: Password Accepted!
UPDATE
After creating a JSON array of length 1 with just the response string and a key for it, and passing it to my android studio code, the json array wont parse through the functions used to parse it (JSON Object, JSON Array), but manually parsing the response string works..... Less graceful then I would like it but i guess it works....
This can happen when encoding of response is not UTF-8. Try converting response like this:
String convertedResponse = null;
try {
convertedResponse = new String(response.getBytes("ISO-8859-1"), "UTF-8");
// do your things ...
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
your response is in JSONObject not JSONArray, so on Response Listnere use:
if(response.getString("response").equals("PASSWORD ACCEPTED!"){
//do what you want to do
}

Problems using scanner to get url content to a string in Android

I'm trying to copy url contents with user info.
First, the user log in and then the page show the user info. I want to copy that info to a String.
I am using this:
try {
variable1 = new Scanner(new URL("https://example.com/hello").openStream(), "UTF-8").useDelimiter("\\A").next();
}catch (MalformedURLException e) {
System.out.println("The URL is not valid.");
}catch (IOException e) {
System.out.println("The URL is not valid.");
}
But I am getting the content in the login page...(as the user never logged in).
I thing maybe missing the cookie id in the request header. The cookie id is in the header of the response after the login request finished.

How to send the values using different button in same activity?

I'm using json parsing in my application first one has following http://shdg.com/webservice/interestreceiveddetail?version=apps&received_detail_id=2783&user_login_id=2650
Here I've so many fields from which I need to use two fields when user click on button
first one is user_status and another one is interest_id.
To send this request I've this URL
http:/fjshdjhs.com/webservice/interestreceivedprocess?version=apps&user_status=N&interest_id=1288
Now i need to send N with user_status,so how to do this..
Please try below code:
String endPoint="http://gujjumatch.com/webservice/interestreceivedprocess?";
String url;
try {
JSONObject resultObject = new JSONObject(response);
String user_status=resultObject.getString("user_status");
int interest_id=resultObject.getInt("interest_id");
if (user_status.equals("Decline")){
user_status="N";
}
String params="version=apps"
+"&user_status="+user_status
+"&interest_id="+interest_id;
url=endPoint+params;
Log.v("URL",url);
} catch (JSONException e) {
e.printStackTrace();
}

Convert Twitter4j Status to JSON Object

Android 2.3.3
I am integrating Twitter API into my android application. I am using Twitter4j.
I could get the tweets for a particular user, using the below code.
Paging paging = new Paging(1, 40);
List<twitter4j.Status> statuses = twitter.getUserTimeline("xxx", paging);
Log.d(Const.TAG,"Showing Timeline.");
for (twitter4j.Status status : statuses) {
String rawJSON = DataObjectFactory.getRawJSON(status);
}
My requirement is that I need to parse the JSON String (rawJSON), to get some other values like, published date and so on.
I am pretty new to JSON parsing esp., with converting things to JSON. All i know in the above code is that, String rawJSON = DataObjectFactory.getRawJSON(status); converts the Status Object to a String and I can view it by logging it.
How do I convert this String to a JSONObject or a JSONArray? Can someone provide me with a sample code?
I tried the below code, but it gives an exception
try {
JSONObject tweet = new JSONObject(rawJSON);
JSONArray textArray = tweet.getJSONArray("text");
String text = textArray.getString(0);
Log.d(Const.TAG, "Text = "+text);
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.d(Const.TAG, "Error while converting string to json");
e.printStackTrace();
}
Here's my code to parse a Twitter4j Status. This example get the language of the tweet.
//Status To JSON String
String statusJson = DataObjectFactory.getRawJSON(status);
//JSON String to JSONObject
JSONObject JSON_complete = new JSONObject(statusJson);
//We get another JSONObject
JSONObject JSON_user = JSON_complete.getJSONObject("user");
//We get a field in the second JSONObject
String languageTweet = JSON_user.getString("lang");
I got a way to get values from Status object without the need to convert it into JSON. Of course this is very basic, by I overlooked it.People who have trouble getting values from Status object, can look at this.
You can get all the values by using the object of Status.
For example, if "status" is an object of Status, we can get the text of the tweet, by using, status.getText() and the user value by using status.user() and so on. Just work on the status object a bit and you will find all the values you need.

Unable to get expected response for Facebook photos from an album

I am new to Facebook API. Trying the FQL Query from the Graph API for the first time using this link.
I am trying to get photos from the album with the album id. When I request using Facebook object with https://graph.facebook.com/10150146071791729/photos&access_token=ACCESS_TOKEN URL, I am getting the following response (before parsing to JSON object). {"id":"https://graph.facebook.com/10150146071791729/photos","shares":2}. And I confirmed it by printing the length of the JSON object after parsing, which is 2. When I copy and paste the same URL in the web browser, I am getting the expected response (the response in FQL Query I got). Here is my code.
public void onComplete(Bundle values) {
String token = facebook.getAccessToken();
System.out.println("Token: " + token);
try {
String response = facebook.request("https://graph.facebook.com/10150146071791729/photos&access_token=ACCESS_TOKEN");
System.out.println("response :"+response);
JSONObject obj = Util.parseJson(response);
System.out.println("obj length : " + obj.length());
Iterator iterator = obj.keys();
while(iterator.hasNext()){
String s = (String)iterator.next();
System.out.println(""+s+" : "+obj.getString(s));
}
} catch (Throwable e) {
e.printStackTrace();
}
}
Note: I got access token from the FQL Query which is used in the URL. And I did not wrote any session (login/logout) logic as it is a test project.
Your request is wrong. It should be
"https://graph.facebook.com/10150146071791729/photos?access_token=ACCESS_TOKEN"
Replace the '&' after the photos with a '?'.
Two more things, you're making a Graph API query, not an FQL one.
Second, NEVER post your access tokens publicly. If I wanted to, I can now use your access token to edit your facebook information.
EDIT: When you use the Android Facebook SDK, you do not need to use the full graph path. Instead, use
facebook.request("10150146071791729/photos")
You do not need to add the access token as the Facebook object already has it. Hope this helps.
Because not much code has been provided except for the most relevant one, let me give you a couple of ways you can access Photos from an Album
FIRST METHOD (IF your wish to use the complete URL to make the request)
String URL = "https://graph.facebook.com/" + YOUR_ALBUM_ID
+ "/photos&access_token="
+ Utility.mFacebook.getAccessToken() + "?limit=10";
try {
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(URL);
HttpResponse rp = hc.execute(get);
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String queryPhotos = EntityUtils.toString(rp.getEntity());
Log.e("PHOTOS RESULT", queryPhotos);
}
} catch (Exception e) {
e.printStackTrace();
}
SECOND METHOD (Without using the complete URL as #Vinay Shenoy mentioned earlier)
try {
Bundle paramUserInfo = new Bundle();
paramUserInfo.putString(Facebook.TOKEN, Utility.mFacebook.getAccessToken());
String resultPhotos = Utility.mFacebook.request("YOUR_ALBUM_ID/photos", paramUserInfo, "GET");
Log.e("PHOTOS", resultPhotos);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
On a personal note, I follow the first method almost entirely through my application. It lets me using the Paging for endless ListViews
That being said, when I need some quick data in between somewhere, I do rely on the second method. Both of them work and I hope either (or both) of them helps you.

Categories

Resources