I have the code which basically retrieving a list of search result based on given keyword from TMDB (API ver.3, new API).
public String getPersonSearchResult(String keywords){
String query = URLEncoder.encode(keywords);
String TMDB_API_URL = "http://api.themoviedb.org/3/search/person?";
String TMDB_LIMIT_LIST = "&page=1";
String TMDB_QUERY = "&query=" + query;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try
{
// ATTEMPT HTTP REQUEST
String fullUrl = TMDB_API_URL + TMDB_API_KEY + TMDB_QUERY + TMDB_LIMIT_LIST;
Log.w(APP_TAG, "TRYING [" + fullUrl + "]");
response = httpclient.execute(new HttpGet(fullUrl));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK)
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
}else{
// FAILED REQUEST - CLOSE THE CONNECTION
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}catch(Exception e){
Log.w(APP_TAG, e.getLocalizedMessage());
Log.w(APP_TAG, "FAILED TO RETRIEVE JSON DATA");
}
return responseString;
}
The problem is that i always get 406 Status Code (Not Acceptable). When i tried to run the URL myself
http://api.themoviedb.org/3/search/person?api_key=<MY_API_KEY_HERE>&query=jennifer&page=1
It displays the JSON result correctly.
I am not sure why is this happening. Similar function is used to retrieve JSON value from other source and and it works perfectly.
this is their API docs regarding search: http://docs.themoviedb.apiary.io/#search
Can anyone points me to the right direction? Any help is appreciated.
I figure it out, by adding this:
HttpGet getObj = new HttpGet(fullUrl);
getObj.addHeader("Accept", "application/json");
Perhaps this is API specific requirement. Not sure though...
One way to do it
Try using builder.scheme
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("api.themoviedb.org").setPath("/3/search/person")
.setParameter("api_key", YOURAPIKEY)
.setParameter("page", 1)
.setParameter("query", query)
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
.....
response = httpclient.execute(new HttpGet(httpget ));
I think its nothing to do with your code.. your code is perfect. The only problem might be with the API request method. Maybe they require some specific headers to be requested for in the request. Give a try with requesting headers like "("Accept", "application/json")" It might work..
try this
String ret = null; try {
response = httpClient.execute(httpGet);
} catch (Exception e) {
e.printStackTrace();
}
try {
ret = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
Log.e("HttpRequest", "" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
return ret;
Related
I have an object that I want to parse to a JSON string. When I want to read it out, it says it can't parse it back to an object (i think). This is the error:
As you can see at the top of the console, the JSON object is set properly with the right brackets. What I understand from the JSONSyntaxException is that it doesn't recognize it as a JSON object. I've copied this code from my school and in a different project it does work. I don't know why it doesn't work in my Maven project.
EDIT
This is the code for RESTcontroller.java.
public PlayerDTO addPlayer(String name, String password) {
PlayerDTO playerRequest = new PlayerDTO(NOTDEFINED, name, password);
String queryPost = "/player";
PlayerResponse response = executeQueryPost(playerRequest, queryPost);
return response.getPlayers().get(0);
}
private PlayerResponse executeQueryPost(PlayerDTO playerRequest, String queryPost) {
final String query = url + queryPost;
log.info("[Query POST] : " + query);
HttpPost httpPost = new HttpPost(query);
httpPost.addHeader("content-type", "application/json");
StringEntity params;
try{
params = new StringEntity(gson.toJson(playerRequest));
System.out.println(gson.toJson(playerRequest));
httpPost.setEntity(params);
} catch (UnsupportedEncodingException e) {
Logger.getLogger(SeaBattleGame.class.getName()).log(Level.SEVERE, null, e);
}
return executeHttpUriRequest(httpPost);
}
private PlayerResponse executeHttpUriRequest(HttpUriRequest httpUriRequest) {
// Execute the HttpUriRequest
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpUriRequest)) {
log.info("[Status Line] : " + response.getStatusLine());
HttpEntity entity = response.getEntity();
final String entityString = EntityUtils.toString(entity);
log.info("[Entity] : " + entityString);
return gson.fromJson(entityString, PlayerResponse.class);
} catch (IOException e) {
log.info("IOException : " + e.toString());
PlayerResponse playerResponse = new PlayerResponse();
playerResponse.setSuccess(false);
return playerResponse;
} catch (JsonSyntaxException e) {
log.info("JsonSyntaxException : " + e.toString());
PlayerResponse playerResponse = new PlayerResponse();
playerResponse.setSuccess(false);
return playerResponse;
}
}
Keep in mind that the code gave me this code and it does work in their project!
It would seem to me that your request is failing with this error: Problem accessing /seabattle/player Reason: request failed. It's not very discriptive, but it's something.
Your code is then failing with a NullPointerException, I'm guessing response.getPlayers() is returning a null.
You should really try and send a similar JSON to this endpoint from a curl command or via Postman app and see if it works. Or if you can, see the webserver logs to find out what the actual problem is.
EDIT: You should really also check the response status before trying to parse it. This will get rid of the NullPointerException and will make your code better in general.
A couple of things to fix the issue:
In the method executeQueryPost you have to add the following headers for the request:
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
And the code should look like the following:
private PlayerResponse executeQueryPost(PlayerDTO playerRequest, String queryPost) {
final String query = url + queryPost;
log.info("[Query POST] : " + query);
HttpPost httpPost = new HttpPost(query);
httpPost.addHeader("content-type", "application/json");
StringEntity params;
try{
params = new StringEntity(gson.toJson(playerRequest));
System.out.println(gson.toJson(playerRequest));
httpPost.setEntity(params);
// Need to add the following headers
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
} catch (UnsupportedEncodingException e) {
Logger.getLogger(SeaBattleGame.class.getName()).log(Level.SEVERE, null, e);
}
return executeHttpUriRequest(httpPost);
}
In case the the code continue failing is because it can't create the new player. You must handle the exception in addPlayer() method. Before the return check if the object returned by executeQueryPost is not null.
Update:
The code may be failing just because the service in: http://localhost:8090 isn't running that's why it getting 500 as response.
I am new in android and java I want to get access data from this API.
All we need to do convert this into java code
curl --include --header "X-Access-Token: YOUR_API_TOKEN_HERE" "http://api.travelpayouts.com/v2/prices/latest?currency=rub&period_type=year&page=1&limit=30&show_to_affiliates=true&sorting=price&trip_class=0"
Your given API has a header and basic GET format. This can be converted in Java easily.
See the code example,
public String httpGet(String s, String api_token) {
String url = s;
StringBuilder body = new StringBuilder();
httpclient = new DefaultHttpClient(); // create new httpClient
HttpGet httpGet = new HttpGet(url); // create new httpGet object
httpGet.setHeader("X-Access-Token", api_token);
try {
response = httpclient.execute(httpGet); // execute httpGet
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
// System.out.println(statusLine);
body.append(statusLine + "\n");
HttpEntity e = response.getEntity();
String entity = EntityUtils.toString(e);
body.append(entity);
} else {
body.append(statusLine + "\n");
// System.out.println(statusLine);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection(); // stop connection
}
return body.toString(); // return the String
}
Now call the function and pass the url along with your header API token,
httpGet("http://api.travelpayouts.com/v2/prices/latest?currency=rub&period_type=year&page=1&limit=30&show_to_affiliates=true&sorting=price&trip_class=0", YOUR_API_TOKEN)
I have an app android that in an AsyncTask make 2 get request to a servlet.
I want to retrieve a String that contains a simple response.
This is my AsyncTask:
protected String doInBackground(Void... params) {
return uploadFile();
}
#SuppressWarnings("deprecation")
private String uploadFile() {
String responseString = null;
String responseStr = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new ProgressListener() {
#Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile = new File(filePath);
// Adding file data to http body
entity.addPart("image", new FileBody(sourceFile));
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
try {
HttpClient client = new DefaultHttpClient();
URI getURL = new URI("http://192.168.1.101:8080/MusaServlet?collection="+collection+"&name="+filename);
Log.i("QUERY",getURL.getQuery());
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}
responseStr = EntityUtils.toString(responseGet.getEntity());
} catch (Exception e) {
e.printStackTrace();
}
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseStr;
}
Instead the servlet code is:
PrintWriter out = response.getWriter();
out.println("HELLO STUPID APP!");
However the dialog showed by app is empty! No words!
What's the problem guys?
Thank's
At first check your GET request status code as
responseGet.getStatusLine().getStatusCode();
If is giving number 200 then GET is successfull.
Now if is 200 then you will get the response what you have sent by following code
HttpEntity resEntityGet = responseGet.getEntity();
and then
String result;
if(resEntityGet !=null ){
result= EntityUtils.toString(resEntityGet);
}
Now the most important thing is once you perform responseGet.getEntity() the data of GET response will be passed to the variable.. you assign.. and later on calling responseGet.getEntity() will always return empty...
That may be the reason you are getting empty response in your dialog
EDIT:
Ok I have modified my code and playing with logcat I'm sure that the responseCode is not 200.
What is the problem now? -.-"
I didn't find this question on StackOverflow or maybe I searched with loose keywords.
I use to get an InputSteam normally for JSON files on Internet, this is my main function:
static HttpGet getRequest;
static HttpResponse getResponse;
public static InputStream retrieveStream(String url, int timeout) {
getRequest = null;
getResponse = null;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout);
HttpConnectionParams.setSoTimeout(httpParameters, timeout);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
getRequest = new HttpGet(url);
try {
getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ERROR", "Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
} catch (IOException e) {
getRequest.abort();
Log.w("ERROR", "Error for URL " + url, e);
} catch (Exception e) {
getRequest.abort();
Log.w("ERROR", "Error for URL " + url, e);
}
return null;
}
for the scope of avoiding errors or giving the possibility to the user to cancel the request because Internet is ON but isn't working fine, I would like to make a counter when my app is not receiving data and it's not completed. I know how to do the counter, but, how could I make that type of 'listener'?
I use to continue with this (always Asynchronously):
InputStream source = f.retrieveStream(params[0]);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
response = gson.fromJson(reader, Usuario.class);
thanks in advance.
When I execute an API through following method, I always get 404 as response code.
private void execute() throws IllegalStateException, IOException, NoSuchAlgorithmException {
Map<String, String> comment = new HashMap<String, String>();
comment.put("accounts-groups", "customers/enterprise");
comment.put("companyType", "customer");
comment.put("companyName", "Test");
String json = new GsonBuilder().create().toJson(comment, Map.class);
Log.i(TAG, "json : "+json);
HttpResponse response = makeRequest(URL, json);
/*Checking response */
if(response != null) {
InputStream inputStream = response.getEntity().getContent(); //Get the data in the entity
int statusCode = response.getStatusLine().getStatusCode();
Log.i(TAG, "statusCode : "+statusCode);
String result;
// convert inputstream to string
if(inputStream != null)
result = convertStreamToString(inputStream);
else
result = "Did not work!";
Log.i(TAG, "result : "+result);
}
}
private HttpResponse makeRequest(String uri, String json) throws NoSuchAlgorithmException {
Log.i(TAG, "uri : "+uri);
try {
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(json, HTTP.UTF_8));
long timestamp = System.currentTimeMillis();
String signatureKey = PRIVATE_KEY + timestamp;
byte[] bytesOfMessage = signatureKey.getBytes(HTTP.UTF_8);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
char[] signature = Hex.encodeHex(thedigest);
String finalSignature = String.valueOf(signature);
Log.i(TAG, "finalSignature : "+finalSignature);
httpPost.setHeader("Timestamp", ""+timestamp);
httpPost.setHeader("Api_token", API_TOKEN);
httpPost.setHeader("Signature" , finalSignature);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");
return new DefaultHttpClient().execute(httpPost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I am not getting where am I going wrong. Can anybody please help me out?
from wiki:
The 404 or Not Found error message is a HTTP standard response code
indicating that the client was able to communicate with the server,
but the server could not find what was requested.
so, your code is OK, but server cannot find resource you are looking for. Double check if your url is correct.
how to pass request through fiddler proxy for debugging purposes:
HttpParams params = new BasicHttpParams();
// ....
HttpHost proxy = new HttpHost("192.168.1.12", 8888); // IP to your PC with fiddler proxy
params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
// use params as a second parameter to: following constructor:
// public DefaultHttpClient (ClientConnectionManager conman, HttpParams params)
I was getting 404 for POST requests because mod_headers module of Apache 2 server was not enabled. If that happens you can enable it with:
sudo a2enmod headers
and then restart apache:
sudo service apache2 restart