I know there are a million threads on this, but I searched many of them with no help, I promise.
I am trying to use Places API to return results. I have tried the following:
Ensure a valid key (it works in this same app where I use a mapFragment, so I know the key is good. The places api was enabled at the time the key was generated.). In addition, I generated a new key and updated the app, map still works.
Make sure the search URL is formed correctly. I copied and pasted from the log into the browser, subbing the browser key, and the results returned appropriately.
Here is the manifest:
<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.berrmal.locationbox.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIza**********************************Y" />
<service
android:name="com.berrmal.locationbox.LocationBoxService"
android:enabled="true"
android:exported="false" >
</service>
</application>
Here is the AsyncTask:
public class PlacesProvider extends AsyncTask<String, Void, ArrayList<Place>> {
//starting arg, progress update type, return from doInBack()
public PlacesProvider() {
}
public ArrayList<Place> doInBackground(String... args) {
//args[0] = search URL
Log.d("lbServ","doing in background");
ArrayList<Place> resultList = new ArrayList<Place>();
DLJSON dLJSON = new DLJSON();
try {
resultList = dLJSON.getResults(args[0]);
} catch (UnsupportedEncodingException uee) {
Log.d("lbServ","unsupp encode ex");
} catch (IOException ioe) {
Log.d("lbServ","IO exception");
} catch (IllegalStateException ils) {
Log.d("lbserv","iill state exception");
ils.printStackTrace();
} catch (Exception ex) {
Log.d("lbServ", "unknown exception in dlJson");
}
if (resultList == null) {
Log.d("lbServ","resultList is null");
}
return resultList;
}
protected void onPostExecute(ArrayList<Place> result) {
Log.d("lbServ","onnPostExecute");
placesList = result;
//TODO do something with the result, i.e. send it to MainActivity via instance that bound to server
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
//resultIntent.setComponent(new ComponentName("com.berrmal.locationbox", "MainActivity"));
resultIntent.putExtra("SearchSuccess", true);
resultIntent.putExtra("searchID", 1);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(resultIntent);
Log.d("lbServ","service suicide");
stopSelf();
}
And the JSON querier/parser
public ArrayList<Place> getResults(String searchURL) throws UnsupportedEncodingException, IOException, IllegalStateException{
try {
url = new URL(searchURL);
Log.d("lbDLJSON",url.toString());
} catch (Exception ex) {
Log.d("lbDLJSON", "malformed URL");
}
try {
urlConn = (HttpsURLConnection) url.openConnection();
Log.d("dlJson","url connection open");
is = new BufferedInputStream(urlConn.getInputStream());
Log.d("dljson","buffered stream open");
} catch (IOException ioe) {
Log.d("lbDLJSON", "io exception");
} catch (Exception e) {
Log.d("lbDLJSON", "that other exception");
}
jsonReader = new JsonReader(new InputStreamReader(is, "UTF-8"));
Log.d("lbjson","json and inputstream readers exists");
resultList = new ArrayList<Place>();
jsonReader.beginObject();
Log.d("lbjson", "top level object open");
while (jsonReader.hasNext()) {
String name = jsonReader.nextName().toString();
if (name.equals("results")) {
jsonReader.beginArray();
while (jsonReader.hasNext()); {
//resultList.add(placeBuilder(jsonReader));
Log.d("lbjson", "results were null");
}
jsonReader.endArray();
} else if (name.equals("status")){
statusCode = jsonReader.nextString().toString();
Log.d("dljson", "status code: " + statusCode);
} else {
Log.d("lbjson", "skipped " + name);
jsonReader.skipValue();
}
}
jsonReader.endObject();
Log.d("lbDLJSON", "top level JSON object closed");
jsonReader.close();
urlConn.disconnect();
return resultList;
//Log.d("lbjson", "");
}
LogCat output:
05-24 16:04:07.859: D/lbServ(19523): search
05-24 16:04:07.859: D/lbServ(19523): executeAsyncTask
05-24 16:04:07.859: D/lbServ(19523): doing in background
05-24 16:04:07.859: D/lbDLJSON(19523): https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIza***************************Y&location=34.0937751,-117.6730797&radius=15000&sensor=true&keyword=food
05-24 16:04:07.859: D/dlJson(19523): url connection open
05-24 16:04:08.199: D/dljson(19523): buffered stream open
05-24 16:04:08.199: D/lbjson(19523): json and inputstream readers exists
05-24 16:04:08.199: D/lbjson(19523): top level object open
05-24 16:04:08.199: D/lbjson(19523): skipped html_attributions
05-24 16:04:08.199: D/lbjson(19523): results were null
05-24 16:04:08.199: D/dljson(19523): status code: REQUEST_DENIED
05-24 16:04:08.199: D/lbDLJSON(19523): top level JSON object closed
05-24 16:04:08.199: D/lbServ(19523): onnPostExecute
05-24 16:04:08.220: D/lbServ(19523): service suicide
I can post the URL generating method, but as you see the URL is formed appropriately from the log.
I have been googling and trying things for several hours, so I'm really stumped. I don't understand why I can paste the same link into the browser, change the key to the browser key, and get valid results (8.3kb), but the app here returns the following 85b result:
{
"html_attributions" : [],
"results" : [],
"status" : "REQUEST_DENIED"
}
I get this same request denied result when clicking the "try it out!" link on the api console list of services, but if I make the URL manually I get valid results...?
EDIT:
I kept the android app key in the manifest, for maps permission, but in the HTTP request, I changed the key to the browser key, and got successful results on the places query. I don't know why, it doesn't really make sense. I'm a bit afraid that its a breach of the TOS, but I'm not really sure. Is this indicative of something I'm doing wrong? Or does anyone know a way to contact Google to report it? Or was I supposed to use both keys all along?
OK, as best I can tell, the issue was solved by using both keys. I use the Android API key in my manifest, this gets the mapFragment working correctly.
To get the places request to come back "OK" and not "REQUEST_DENIED", I had to use the browser key from the Google API console. I assume, because my app constructs a search URL and uses the HTTP connection to send and receive, google's server thinks its a browser rather than an app. I don't know if this is correct usage, if I get a nastygram from Google I'll update the thread.
Related
I created a wallpaper (for my Android 4.0.3) but I have a problem with the preferences.
When I try to invoke a service http-get from PreferenceFragment, I do not get any response. In practice, it seems that call is lost. I tried with a call like:
InputStream stream = null;
StringBuilder fos = null;
try {
URL url = new URL("http://.....");
stream = url.openConnection().getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
fos = new StringBuilder();
int next = -1;
while ((next = reader.read()) != -1) {
fos.append((char)next);
}
} catch (Exception e) {
// ...
} finally {
// ...
}
or with an external framework like libgdx:
HttpRequest httpRequest = new HttpRequest(HttpMethods.GET);
httpRequest.setUrl("http://.....");
NetJavaImpl net = new NetJavaImpl();
net.sendHttpRequest(httpRequest, this);
but nothing happened. On the device no trace about this or a probability exception is present.
I have a manifest like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.mytest.live"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-feature
android:name="android.software.live_wallpaper"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MyWallpaperSettings"
android:exported="true"
android:hardwareAccelerated="false"
android:label="MyWallpaperSettings"
android:permission="android.permission.INTERNET" />
<service
android:name=".MyWallpaper"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:permission="android.permission.BIND_WALLPAPER" >
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="#xml/livewallpaper"
android:value="true" />
</service>
</application>
</manifest>
Do you have any idea? I'm going crazy :(
*edit: Solved, Thanks!! *
private class SendAsyncTask extends AsyncTask<String, String, String>
{
protected String doInBackground(String... status)
{
String result = "";
try
{
String url = "my magic http-get";
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
if(response != null && response.getEntity() != null)
{
InputStream stream = response.getEntity().getContent();
try
{
// parse an XML with libgdx ...
XmlReader xmlReader = new XmlReader();
Element root = xmlReader.parse(stream);
Array<Element> childs = root.getChildrenByNameRecursively("mykids");
for (Element child: childs)
{
Element myelement = child.getChildByName("name");
//do something...
}
}
catch (Exception e)
{
//
}
finally
{
try
{
stream.close();
}
catch (Exception e)
{
//
}
}
}
}
catch (Exception e)
{
//
}
return result;
}
protected void onPostExecute(String result)
{
super.onPostExecute(result);
}
}
You could possibly get a NetworkOnMainThreadException because you made a network connection in a PreferenceFragment, which runs on main UI thread.
If so, you could move network jobs to a background service. Note that Service runs on main UI thread too. So you need Thread inside the service.
A side note: it seems you swallowed all exceptions with:
try {
// ...
} catch (Exception e) {
// ... => HERE
}
those of you who read my previous questions may already know this: I'm currently trying to get a hold on the whole WiFi Direct stuff. What I've done so far is creating the connection.
What I'm trying to do is sending a packet via UDP to the groupOwner (which's IP is of course known) - but it seems to get lost on it's way each time..
Here's a bit of my code, I've got a service for receiving files and an intentservice for submitting them in the background:
FileReceiverService.java
#Override
public void run() {
Log.d(TAG, "Thread starting...");
try {
app.log("Opening UDP socket to receive files.");
DatagramSocket socket = new DatagramSocket(PORT);
app.log("Socket open!");
socket.setSoTimeout(5000);
app.log("Waiting for packet..");
while (isRunning && !isInterrupted()) {
DatagramPacket packet = new DatagramPacket(
new byte[WiFiPacket.PACKET_SIZE],
WiFiPacket.PACKET_SIZE);
try {
socket.receive(packet);
app.log("Received " + packet.getLength()
+ " bytes, trying to parse!");
parsePacket(packet);
} catch (SocketTimeoutException e) {
} catch (Exception e) {
app.log("Something went wrong: " + e.getMessage());
e.printStackTrace();
}
}
socket.close();
app.log("Closing UDP socket");
} catch (Exception e) {
e.printStackTrace();
}
}
Where the constant WiFiPacket.PACKET_SIZE is set to 1024*32 (32 KBytes, because I've received "ERRBLABLA MSG too long" errors with higher values).
FileTransferService.java
#Override
protected void onHandleIntent(Intent intent) {
App app = (App) getApplication();
Context context = getApplicationContext();
boolean rightIntent = false;
WiFiFile file = null;
if (intent.getAction().equals(ACTION_SEND_TEXT)) {
rightIntent = true;
file = WiFiFile.fromText(intent.getExtras().getString(EXTRAS_TEXT));
} else if (intent.getAction().equals(ACTION_SEND_FILE)) {
rightIntent = true;
file = WiFiFile.fromFile(intent.getExtras().getString(
EXTRAS_FILE_PATH));
}
if (rightIntent && file != null) {
app.getOnWiFiTransmissionChangedListener().onNewOutgoingTransfer(
file);
String text = intent.getExtras().getString(EXTRAS_TEXT);
String host = intent.getExtras().getString(
EXTRAS_GROUP_OWNER_ADDRESS);
DatagramSocket socket = null;
int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
Log.d(TAG, "Sending packets to " + host + ":" + port);
try {
socket = new DatagramSocket();
int bytesSent = 0;
for (WiFiPacket p : file) {
Log.d(TAG, "Preparing another packet..");
byte[] payload = p.getBytes();
DatagramPacket packet = new DatagramPacket(payload,
payload.length, InetAddress.getByName(host), port);
Log.d(TAG, "Sending packet..");
socket.send(packet);
bytesSent += payload.length;
Log.d(TAG, "Packet send! Contained " + payload.length
+ " bytes! All over we've sent about " + bytesSent
+ " bytes!");
List<WiFiFile> list = new ArrayList<WiFiFile>();
list.add(file);
app.getOnWiFiTransmissionChangedListener()
.onTransferProgressChanged(list);
}
app.getOnWiFiTransmissionChangedListener()
.onFileTransferSuccessful(file);
Log.d(TAG, "Client: Data written");
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
} finally {
if (socket != null) {
if (socket.isConnected()) {
socket.close();
}
}
}
}
}
The code may not be the best, but as for right now the only thing I care about is to receive the damn packet. :)
For your information: I'm splitting instances of the class WiFiFile into WiFiPackets which do not exceed the PACKET_SIZE limit, thus contain some sort of own header information about the file's offset, the total length, the sender (username/inetaddress) and stuff like that.
My logging tells me that about 25KBytes were sent, the socket.send does not throw any errors but instead calls my listeners (also the one telling me the transfer is completed).
As far as I know, UDP packets can easily be dropped on their way, but I've tried it roughly 10 times and I think the probability of a 25KB packet getting lost EVERYTIME is very small.
Do you see anything I'm missing? I'm staring at my code since hours, trying to figure it out, putting more and more debug/log statements in it, but without any progress.
Thanks!
PS:
IP addresses and port are correct.
PS2:
AndroidManifest.xml
<uses-feature
android:name="android.hardware.wifi.direct"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE" />
Okay, I solved this problem.
I sent the messages directly after receiving the callback for a successful group creation, thinking that the group really would be "ready"... Solved this by sending out an initial message in a TimerTask every ~200-500ms, cancelling the Task after receiving ack.
Hope this helps some of you, facing the same problem. :)
i am new android developer
when ever i run this code i get this error "Source not found." just when it reaches the
url.openStream()
any idea how to fix this?
try {
URL url = new URL("http://pollsdb.com/test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
<uses-permission
android:name="android.permission.INTERNET" />
add that under your first manifest tag in your AndroidManifest.xml
I have the same problem too. But now, i have been solved this problem by reference http://developer.android.com/guide/components/processes-and-threads.html
about the thread use.
You need to create a worker thread to work for open the URL stream rather than using the main thread(UI thread).
Of course, you also need to add
<uses-permission android:name="android.permission.INTERNET" />
in your AndroidManifest.xml
Snippet provided:
public void update(){
try {
Socket appSoc = new Socket( "XXX.XXX.XXX.X" ,XXXXX);
BufferedReader in = new BufferedReader(new
InputStreamReader(appSoc.getInputStream()));
for (int i = 0; i < 100; i++) {
String message = in.readLine();
add(message);}
}
catch (Exception e) {
add("ERROR" + e);
}
}
add(String text) adds text to a textview.
Using the domain name instead of the IP address says that the phone cannot find the domain, is this an android problem, because it runs fine on java on the desktop.
You are probably missing the Internet persmission in your manifest. Make sure it is located outside of the application tag, like this:
<manifest>
<application>
.
.
.
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
In your androidmanifest.xml, check to see if you have given proper internet permissions.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
I am trying to integrate twitter api using library twitter4j-core-2.1.1-SNAPSHOT.jar. But i am getting error Unknown host Exception from 2 days. here is the part of code(index by .....(1)) that is generating Exception.
public String beginAuthorization() {
//showDialog("Authorization...........");
//new AuthorizationTask(this).execute("Authorize");
try {
if (null == currentRequestToken) {
Log.e("begin","i am here the line below is throwing excetion");
currentRequestToken = twitter.getOAuthRequestToken();............(1)
}
Log.e("beginAuthorization", currentRequestToken.getAuthorizationURL());
return currentRequestToken.getAuthorizationURL();
} catch (TwitterException e) {
e.printStackTrace();
}
return null;
}
anyone have idea about this problem???
thank u in advance
Did you put permission in xml manifest?
Have you tried opening a browser in android and going to twitter.com?
Try to connect to any url from the code and see what happens.
This is what I'd forgotten to include in my AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />