This is going to be a bit long but hopefully others will also benefit from this thread.
I am planning to make a private application for quite a number of people. It is NOT going to be on the google play. Thus, I am now facing a few problems: How do I download the new version and how do I re-install it?
I have read some brilliant websites about installing and uninstalling applications with another app like:
http://android.amberfog.com/?p=98
and some about downloading app from online:
Download a file with Android, and showing the progress in a ProgressDialog
and even this:
Auto-Update for (private) Android apps
But I can't figure out how to do what I want to. I want to create an app that can auto-update. My plan, from researching over and over, is to have 2 applications. The main one needs to be updated, while the second one is always there. Each time the main one is on, the second app is called and perform checks and if necessary, uninstall the main app. Then, it goes on to download the new version of the main app to the phone, and install it.
Here are some codes of the downloading process that I'm stuck in:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"http://dl.dropboxusercontent.com/u/98196898/data.json");
try{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new
InputStreamReader (content));
String line;
while((line = reader.readLine()) != null) {
builder.append(line);
}
}
}catch (Exception e)
{
e.printStackTrace();
}
TextView tv = (TextView)findViewById(R.id.showresponse);
tv.setText(builder.toString());
try{
String answer = "";
JSONArray jsonArray = new JSONArray(builder.toString());
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
answer += jsonObject.getString("VersionNumber");
}
tv.setText(answer);
Which, doesn't work. I do not know why. My .json is made with a notepad with the following:
{
"VersionNumber": "2"
"VersionCode": "2.0"
}
And my plan is to compare the version number with the old one--> obviously if the one online has a bigger number than it needs to be updated.
And, of course, it doesn't work. I've also tried to use defaulthttpclient and httpget and stuff but it doesn't work. Maybe dropbox has this problem?
Next, I realized that I am wasting my time so I moved on to installing the apk ASSUMING THAT one day I will figure out how to download things from the net. I tried, and it doesn't work either. Here are the codes and problems:
String fileName = Environment.getExternalStorageDirectory()
+ "/Android/CheckWebForUpdate.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);
I copy most of them here online, but it doesn't work. It prompted: "There is a problem parsing the package". I saw two ways of solving this but neither worked. I looked back it up and my codes should work. Someone mentioned that I should not have the same name, so I tried another program's name, and of course, it doesn't work. Then, I looked up again and this time I figured out that I "might" need permission android.permission.INSTALL_PACKAGES but then I figured out that I need to root my phone (and because the app is not just for me... I need to root LOTS of phones, which just does not make sense)-- It is only for system apps.
I am stuck here in two ways. Thank you for reading. Please reply if you know how to solve these problems.
Related
I am developing an app that logs onto a tomcat server.
I am using a HTTP GET request to do so, and upon successful connection,
the message is displayed via a buffered stream.
The following code, is used to connect.
public String getInternetData() throws Exception {
BufferedReader in = null;
String data = null;
try {
HttpClient client = new DefaultHttpClient();
client.getConnectionManager().getSchemeRegistry().register(getMockedScheme());
URI website = new URI("https://ts.rks.com:8443/kss/login?username=hydm&password=pw1234");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
response.getStatusLine().getStatusCode();
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} finally {
if (in != null) {
try {
in.close();
return data;
} catch (Exception e) {
Log.e("GetMethodLogin", e.getMessage());
}
}
}
This is the code that is activated when the user logs in via a login activity.
When I go back to the menu screen and try to run another activity that requires the user
to be logged in, it says that the user is not logged in.
Is the connection being disconnected when the user moves away from the activity or am I not establishing the connection correctly.
There are actually two things you need to change, not just one.
Anything that you need to have persistent across Activities should be done in a Service, not in the code of an Activity.
Second, on recent Android releases, all networking must be done from background threads, rather than the UI thread (there was always a recommendation against UI thread networking, but now it triggers an exception). Just putting the code in a Service does not mean it is in another thread.
So the answer is that you should be doing this using one of the background threading mechanisms, and doing that within a Service.
The connection will not persist when the activity is moved to the background. I cannot tell from your posted code, but I believe you should be using an AsyncTask for this connection. This is coming from my experience pulling HTML source from a link.
This question may answer yours: What to do with AsyncTask in onPause()?
This must be something really stupid, trying to solve this issue for a couple of days now and it's really not working. I searched everywhere and there probably is someone with the same problem, but I can't seem to find it.
I'm working on an Android app and this app pulls some xml from a website. Since this website is down a lot, I decided to save it and run it locally. Now what I did:
I downloaded the kWs app for hosting the downloaded xml file.
I put the file in the right directory and could access it through the mobile browser, but not with my app (same code as I used with pulling it from some other website, not hosted by me, only difference was the URL obviously).
So I tried to host it on my PC and access it with my app from there. Again the same results, the mobile browsers had no problem finding it, but the app kept saying 404 Not Found: "The requested URL /test.xml¶ma=Someone¶mb= was not found on this server."
Note: Don't mind the 2 parameters I am sending, I needed that to get the right stuff from the website that wasn't hosted by me.
My code:
public String getStuff(String name){
String URL = "http://10.0.0.8/test.xml";
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("parama", name));
params.add(new BasicNameValuePair("paramb", ""));
APIRequest request = new APIRequest(URL, params);
try {
RequestXML rxml = new RequestXML();
AsyncTask<APIRequest, Void, String> a = rxml.execute(request);
...
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
That should be working correctly. Now the RequestXML class part:
class RequestXML extends AsyncTask<APIRequest, Void, String>{
#Override
protected String doInBackground(APIRequest... uri) {
HttpClient httpclient = new DefaultHttpClient();
String completeUrl = uri[0].url;
// ... Add parameters to URL ...
HttpGet request = null;
try {
request = new HttpGet(new URI(completeUrl));
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
HttpResponse response;
String responseString = "";
try {
response = httpclient.execute(request);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
// .. It crashes here, because statusLine.getStatusCode()
// returns a 404 instead of a 200.
The xml is just plain xml, nothing special about it. I changed the contents of the .htaccess file into "ALLOW FROM ALL" (works, cause the browser on my mobile device can access it and shows the correct xml).
I am running Android 4.0.4 and I am using the default browser AND chrome on my mobile device.
I am using MoWeS to host the website on my PC.
Any help would be appreciated and if you need to know anything before you can find an answer to this problem, I'll be more than happy to give you that info.
Thank you for you time!
Cheers.
I think you just miss the question mark after your filename
/test.xml?¶ma=Someone¶mb=
I'm having a very odd issue with getting data from a PHP script in Android. The below example seams to be what everyone suggests to use to get the data. And it works.. for a while. If you run it a couple of times it gets the scores perfectly. But if you wait an hour or two and then try again it will fail over and over again. Nothing actually "fails" though, I do have proper try catch around this and it runs in a Async task, it just returns nothing in the string. This will happen a hundred times, until I open the script on my computer. Then the script shows me the data and the very next time I open in on the phone, it works. Until a few hours later...
I have scoured the internet for the last week, trying ever different example of getting data from the page and they all do the same thing. I even though maybe it was the web-server we are on, so I moved to a new one, same issue.
If you need anymore details let me know. I can give you the PHP script if you think that will help. Although it happens with 5 different scripts that I have.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log(json);
Okay, so I was trying to send Http Post Requests to this one site, and I sniffed the sent request with wireshark thus getting the text data from the post request of this site. I used this in a stock Java application, and it worked perfectly fine. I could use the post method regularly with no problem whatsoever, and it would return the appropriate website. Then I tried doing this with Android. Instead of returning the actual html data after executing the post request, it returns the regular page html data untouched. It DOES send a post request (sniff with wireshark again), it just doesn't seem to get the appropriate response. I took the exact same method used from another one of my projects, which worked perfectly fine in that project, and pasted it into my new project. I added the INTERNET user permission in Android, so there's nothing wrong with that. The only visible difference is that I used NameValuePairs in the other one (the one that worked) and in this one I'm directly putting the string into a StringEntity without encoding (using UTF-8 encoding screws up the String though). I used this exact same line of text in regular Java like I said, and it worked fine with no encoding. So what could be the problem? This is the code:
public static String sendNamePostRequest(String urlString) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
StringBuffer sb = new StringBuffer();
try {
post.setEntity(new StringEntity(
"__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE3NDM5MzMwMzRkZA%3D%3D&__EVENTVALIDATION=%2FwEWBAL%2B%2B4CfBgK52%2BLYCQK1gpH7BAL0w%2FPHAQ%3D%3D&_nameTextBox=John&_zoekButton=Zoek&numberOfLettersField=3"));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(
entity.getContent()));
String in = "";
while ((in = br.readLine()) != null) {
sb.append(in + "\n");
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
Can you see what's wrong here?
I am trying to be able to view the source code of a webpage after being given a URL in order to parse the text for a certain string which represents and image url.
I found this post which is pretty much what I am after trying to do but can't get it working:
Post
This is my code below.
public String fetchImage() throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("www.google.co.uk/images?q=songbird+oasis");
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
return html;
}
but for some reason it just does not work. It forces me to use a try catch statement in calling the method. Once this works I think it will simple from here using regex to find the string "href="/imgres?imgurl=........jpg" to find the url of a jpg image to then be shown in an image view.
Please tell me if i'm going at this all wrong.
First, Google has a search API, which will be a better solution than the scraping you are going through, since the API will be reliable, and your solution will not be.
Second, use the BasicResponseHandler pattern for string responses, as it is much simpler.
Third, saying something "just does not work" is a pretty useless description for a support site like this one. If it crashes, as kgiannakakis pointed out, you will have an exception. Use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine the stack trace and find out what the exception is. That will give you some clues for how to solve whatever problem you have.