Sorry I am a newbie with Android developing an app which sell digital downloads. I am using Volley Library to post INAPP_PURCHASE_DATA and INAPP_DATA_SIGNATURE to webserver to verify order and issue download link passing
http://domain.com/thankyou.php?purchaseData="+purchaseData+"&dataSignature="+dataSignature+"
If the order is verified a download link will be issued inside the page. The file name isn't static which means it's auto-generate by system so I can't guess the name each time issued from the server side. Can be file1.dat file2.dat file3.dat each time has a name depending on certain parameters I defined into the webserver.
<html>
<head>
<title>Thank You</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<a href="http://domain.com/file.dat" title="Order" download>Download File</a>
</body>
</html>
Now I need to post the download file link to the app so the customer can download the file.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//download file on through a new in-app page
Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
})
I have created an activity_download.xml file and a new Download Class.
package com.example.app;
import java.util.List;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.app.R;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Download extends Activity {
private String filename;
#Override
protected void onCreate (final Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_download);
}
}
}
Sorry again I am a newbie with Android. Can you guide me a provide me with a way to have a complete Download.java class as a new page and then call it from the MainActivity.java side onresponse and successful result.
Thank you so much!
Cange your MainActivity code to this.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Intent intent = new Intent(MainActivity.this,Download.class);
startActivity(intent);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
})
As you want make a toast as soon as you get a Respons. dont do it in Activity class, do it in Download class becasue you will redirected to Download class soon as you get the response
public class Download extends Activity {
private String filename;
#Override
protected void onCreate (final Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_download);
//download file on through a new in-app page
Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
}
}
}
Related
I have the following code , im receiving an error :
enter image description here
package com.example.photopicker;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button addimage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addimage=findViewById(R.id.button_pick_photo);
addimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Registers a photo picker activity launcher in single-select mode.
ActivityResultLauncher<PickVisualMediaRequest> pickMedia =
registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), uri -> {
// Callback is invoked after the user selects a media item or closes the
// photo picker.
if (uri != null) {
Log.d("PhotoPicker", "Selected URI: " + uri);
} else {
Log.d("PhotoPicker", "No media selected");
}
});
// Include only one of the following calls to launch(), depending on the types
// of media that you want to allow the user to choose from.
// Launch the photo picker and allow the user to choose images and videos.
pickMedia.launch(new PickVisualMediaRequest.Builder()
**.setMediaType(new ActivityResultContracts.PickVisualMedia.ImageAndVideo())**
.build());
}
});
}
}
This code i got it from the Android developer Website :
https://developer.android.com/training/data-storage/shared/photopicker
but Doesnt seem to work , and im not able to find any online solution.
Try replacing:
new ActivityResultContracts.PickVisualMedia.ImageAndVideo()
with:
ActivityResultContracts.PickVisualMedia.Companion.getImageAndVideo()
ImageAndVideo is a Kotlin object — it is not a class that you instantiate yourself. However, the source code lacks the #JvmField annotation, so I think that just referring to ActivityResultContracts.PickVisualMedia.ImageAndVideo will fail, as outlined in the docs.
I have written this code in Android Studio but as soon as I launch the app it crashes.
package PACKAGE_NAME;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.v2.DbxClientV2;
public class MainActivity extends AppCompatActivity {
private static final String ACCESS_TOKEN = MY_ACCESS_TOKEN;
DbxRequestConfig config = DbxRequestConfig.newBuilder("db").build();
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
((TextView)findViewById(R.id.textView)).setText(client.users().getCurrentAccount().getName().getDisplayName());
} catch (DbxException e) {
e.printStackTrace();
}
}
}
I would like to connect to my Dropbox account via access token, I did a similar thing in python and it actually worked.
Someone who knows why this happens?
I am a beginner, building a goofy app that uses an online API. I uploaded the .json file to GitHub, from where my app accesses it and gives the output. However, it always runs the statements for onErrorResponse. Am I doing something wrong?
Here's the code for MainActivity:
package com.example.jsonparsing;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DownloadManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
RequestQueue requestQueue;
Button search;
EditText input;
TextView word;
TextView meaning;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = findViewById(R.id.button_getMeaning);
input = findViewById(R.id.editText_input);
word = findViewById(R.id.textView_word);
meaning = findViewById(R.id.textView_meaning);
requestQueue = Volley.newRequestQueue(this);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String term = input.getText().toString().toLowerCase();
String displayInput = term.toUpperCase() + ":";
word.setText(displayInput);
JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.GET, "https://github.com/DeathVenom54/bonerDictionaryDatabase/blob/master/definitions.json", null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
String definition = response.getString(term);
meaning.setText(definition);
} catch (JSONException e) {
e.printStackTrace();
meaning.setText("Sorry, this word doesn't exist in the boner database.");
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Connection error, please check internet connection.", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest1);
}
});
}
}
The project uses Volley and I have added Internet permission in the manifest.
Edit: here's the .json :
https://github.com/DeathVenom54/bonerDictionaryDatabase/blob/master/definitions.json
The json url you have posted is a link to someone's github page with the json, but not the json itself. Whats most likely happening is volley is downloading the HTML page in its entirety which contains the json. Its then complaining that its not correctly formatted json.
If you want that json directly you need to find a way to host it somewhere. For example github has an api that you can access json files like this https://api.github.com/users/mralexgray/repos
For local testing you could always copy it into a local json file and import into the res/raw of your app and reference it from there.
I don't know if it's possible but in my app I got an activity that show a phone number (retrieve from the web).
Can I send this number to the main phone app of android? For example to call it or save it?
You can create a tel:... Uri, where the ... is replaced by your phone number, then use that Uri with ACTION_DIAL or ACTION_CALL. For example:
package com.commonsware.android.dialer;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class DialerDemo extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
public void dial(View v) {
EditText number=(EditText)findViewById(R.id.number);
String toDial="tel:"+number.getText().toString();
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(toDial)));
}
}
The code shown above is from this sample project.
I'm trying to use the Android tutorial to build an app that loads a web view to a mobile site that I built. The problem is with following the tutorial the startActivity function is undefined and the Android tutorial isn't helping. I've done Ctrl+Shift+O to verify all the proper modules are loaded.
package com.mysite;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.mysite.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Update
Ok, now my code reads:
package com.myapp;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MyApp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init webview
WebView DCWebView = (WebView) findViewById(R.id.webview);
WebSettings webViewSettings = DCWebView.getSettings();
//when a link is clicked, use the WebView instead of opening a new browser
DCWebView.setWebViewClient(new MyWebViewClient() {
#Override
public void launchExternalBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
//enable javascript
webViewSettings.setJavaScriptEnabled(true);
}
}
But I'm showing 2 errors:
Description Resource Path Location Type
The type new MyWebViewClient(){} must implement the inherited abstract method MyWebViewClient.launchExternalBrowser() DealClippings.java /MyApp/src/com/myapp line 21 Java Problem
The method launchExternalBrowser(String) of type new MyWebViewClient(){} must override or implement a supertype method MyApp.java /DealClippings/src/com/myapp line 23 Java Problem
There really is no startActivity method for WebViewClient. You can check the docs. You'll have to signal the Context (probably your Activity) to execute those lines of code instead. There are many possible approaches including adding listeners or simply calling an abstract method which you implement in an anonymous instance of this class when setting the WebViewClient of your WebView in your Activity.
For example:
public abstract class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.mysite.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
launchExternalBrowser(url);
return true;
}
public abstract void launchExternalBrowser(String url);
}
And then in your activity:
WebViewClient client = new MyWebViewClient() {
#Override
public void launchExternalBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
};
Although I'm not sure why you want this behavior exactly, but it should work more or less.
I don't know if there's much point answering your edit now, 18 months on, but it seems like this question gets a bit of traffic so I'll post this here for posterity.
From your errors, it sounds like you haven't provided an argument to the abstract method in the abstract class definition of MyWebViewClient. That is, you have this:
public abstract void launchExternalBrowser();
when you should have this:
public abstract void launchExternalBrowser(String url);
The cause of the error is that Java treats two methods with the same name but different arguments as two distinct methods. So launchExternalBrowser(String) is a different method to launchExternalBrowser().
Hope this helps someone!
To answer original question before your edit..
Had the same problem, and figured out that the MyWebViewClient is meant to be an inner class inside the activity.
package com.myapp;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyApp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init webview
WebView DCWebView = (WebView) findViewById(R.id.webview);
WebSettings webViewSettings = DCWebView.getSettings();
//when a link is clicked, use the WebView instead of opening a new browser
DCWebView.setWebViewClient(new MyWebViewClient());
//enable javascript
webViewSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.mysite.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}