I am developing an Android Application (Android Studio - Java) which includes a sign in and registration process. I took care of the sign in and registration processes by implementing a connection between PHP files and a MySQL database through http. In short, I just created an AsyncTask class in java --called from another class-- and used it to post data to a PHP file, from there I just used the appropriate SQL commands. This part works fine.
This first part with the login and registration is important because every user will see a slightly different layout once they login. The layout is a RecyclerView composed of detailed CardView elements. Each CardView has a few TextViews with some details. The details are held by an Object i created in a separate class. To fill in the CardView elements a fetched some JSON data using a separate PHP file (and one more http connection). Parsing the data from JSON into Strings and ints was a straightforward endeavor, as was adding them to the list of custom objects. There is some code below showing how I fetched the data and added it to the Object list.
This is the complete AsyncTask class:
public class ScheduleWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
ScheduleWorker (Context ctx) { context = ctx; }
#Override
protected void onPreExecute() { super.onPreExecute(); }
#Override
protected void onPostExecute (String s) {
super.onPostExecute(s);
Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
try {
loadJSON(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
protected String doInBackground(String... params) {
final String fetch_url = "http://192.168.1.70/newfetcher.php";
try {
String ussr_name = params[0];
URL url = new URL(fetch_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
OutputStream outputStream = con.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(ussr_name, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
private void loadJSON(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
Course course;
List<Course> courses = new ArrayList<Course>();
int len = jsonArray.length();
String[] titles = new String[len];
String[] types = new String[len];
String[] teachers = new String[len];
int[] pics = new int[len];
for (int i = 0; i < len; i++) {
JSONObject obj = jsonArray.getJSONObject(i);
titles[i] = obj.getString("title");
types[i] = obj.getString("type");
teachers[i] = obj.getString("teacher");
pics[i] = obj.getInt("pic");
course = new Course(titles[i], types[i], teachers[i], pics[i]);
courses.add(course);
}
}
}
Now, where I encountered problems is when I tried saving the List of Objects (and their details) to a Room Persistent Database. I based my code for the Room Database off an example from Google Developer CodeLabs. I adapted the code for my particular needs but I kept the underlying class structure. The structure includes: an Entity, a DAO, a RoomDatabase, a Repository, a ViewModel, a ViewHolder, an Adapter for the RecyclerView, and a class to populate the database. Everything seems fine except for the part where I populate the database. The example populates the database by using a callback and an AsyncTask within the RoomDatabase class.
Here is populating AsyncTask:
private static RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback() {
#Override
public void onOpen(#NonNull SupportSQLiteDatabase db) {
super.onOpen(db);
// If you want to keep the data through app restarts,
// comment out the following line.
new PopulateDbAsync(INSTANCE).execute();
}
};
/**
* Populate the database in the background.
* If you want to start with more words, just add them.
*/
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
private final WordDao mDao;
PopulateDbAsync(WordRoomDatabase db) {
mDao = db.wordDao();
}
#Override
protected Void doInBackground(final Void... params) {
// Start the app with a clean database every time.
// Not needed if you only populate on creation.
Word word = new Word("Hello");
mDao.insert(word);
word = new Word("World");
mDao.insert(word);
return null;
}
}
My question is how do I populate the database with the detail arrays and/or the Object list? The example executes the callback every time a adding activity is called. I just want to populate the database when the user registers.
Related
I was trying to write an app which gets some JSON from a website, converts it into an array and then puts specific elements of this array into different spots of a table in the app. I wrote the code which gets me the JSON data and converts it into an ArrayList in Eclipse. It worked great there. I now tried to implement the code in Android Studio but I just can't get it to work. I have the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);
TextView textview = (TextView) findViewById(R.id.textView8);
try{
textview.setText(MainActivity.get_data().toString());
}
catch (Exception e){
textview.setText(e.getClass().getCanonicalName());
}
}
public static ArrayList<String> get_data() throws Exception{
String[] ids = new String[] {(here are some ids)};
ArrayList<String> alldata = new ArrayList<>();
for(int r=0;r<ids.length;r++) {
String url = "https://ktrax.kisstech.ch/backend/tracking?db=aviation&sw_lat=40.97765930663377&sw_lon=-26.280096606906117&ne_lat=43.01854550507729&ne_lon=-23.407171802218617&ktrax_id=icao%3a" +ids[r]+"&format=json";
URL ktraxURL = new URL(url);
JSONArray test = Networkaccess.getJSONarr(ktraxURL);
ArrayList<String> listdata = MainActivity.converter(test);
ArrayList<String> elementlist = new ArrayList<>();
(some more code (irrelevant in this matter) which makes the array nice and neat)
//System.out.println(alldata);
return alldata;
}
public static ArrayList<String> converter(JSONArray test){ //creates an array with all the data splited
(Some code that converts JSON to ArrayList (also no problems here))
}
}
class Networkaccess{
public static JSONArray getJSONarr(URL ktraxURL) throws Exception{
HttpURLConnection con = (HttpURLConnection) ktraxURL.openConnection();
//Checking for reponse code of GET Method
con.setRequestMethod("GET");
!!!!!! int responseCode = con.getResponseCode(); //THE DEBUGGER JUMPS FROM HERE TO THE CATCH IN MAIN
System.out.println("Response Code : " +responseCode);
//Reading Data and
BufferedReader readin = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = readin.readLine()) != null) {
response.append(inputLine);
}
readin.close();
//System.out.println(response.toString());
String jsonresp = new String(response.toString());
//System.out.println(jsonresp);
JSONObject myResponse = new JSONObject(jsonresp);
JSONArray test = myResponse.getJSONArray("targets");
//System.out.println(test);
return test;
}
}
I created the new network class as the error, which was displayed in the textview field was: android.os.NetworkOnMainThreadException. However, this didn't seem to do the job and the same error is still shown. I have no idea on how to continue from here. When debugging, the debugger always runs until the line which I marked with !!! in the code. Then it jumps to the catch exception in the main activity.
Looking forward to your answers!
Networking stuff tends to brick up, since responses can take a while to travel. What it's saying is that you need to move your code to another thread so that your program doesn't freeze up while it's waiting for a response.
This won't prevent networking-related slowdowns, but it will get rid of the error:
Thread networking = new Thread(){
void run() {
int responseCode = con.getResponseCode();
}
}
networking.start();
networking.join();
I'm trying to build a very basic weather app in android studio. I am using AsyncClass to return multiple strings.
As you can see in the code, I used a class named "Wrapper" that is used to store my strings so I can just return a class object and use it in the onPostExecute method of the AsyncTask. The problem I am facing is that when I test the app, all of the returned Strings somehow are undefined (the default for the Wrapper class). This means the strings are not being updated in the doInBackground method and I can't seem to figure out why!
My Activity
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(MainActivity.class.getSimpleName(), "Can't connect to Google Play Services!");
}
private class Wrapper
{
String Temperature = "UNDEFINED";
String city = "UNDEFINED";
String country = "UNDEFINED";
}
private class GetWeatherTask extends AsyncTask<String, Void, Wrapper> {
private TextView textView;
public GetWeatherTask(TextView textView) {
this.textView = textView;
}
#Override
protected Wrapper doInBackground(String... strings) {
Wrapper w = new Wrapper();
String Temperature = "x";
String city = "y";
String country = "z";
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String inputString;
while ((inputString = bufferedReader.readLine()) != null) {
builder.append(inputString);
}
JSONObject topLevel = new JSONObject(builder.toString());
JSONObject main = topLevel.getJSONObject("main");
JSONObject cityobj = topLevel.getJSONObject("city");
Temperature = String.valueOf(main.getDouble("temp"));
city = cityobj.getString("name");
country = cityobj.getString("country");
w.Temperature= Temperature;
w.city= city;
w.country=country;
urlConnection.disconnect();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return w;
}
#Override
protected void onPostExecute(Wrapper w) {
textView.setText("Current Temperature: " + w.Temperature + " C" + (char) 0x00B0
+"\n" + "Current Location: "+ w.country +"\n" + "City: "+ w.city );
}
}
}
UPDATE:
turned out that that I was using the wrong url in my code,I was using :
http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=%s&appid=%s
Instead I should've been using:
http://api.openweathermap.org/data/2.5/forecast?lat=%f&lon=%f&units=%s&appid=%s
-aka instead of weather I should've been using forcast
Your error starts here
JSONObject main = topLevel.getJSONObject("main");
Probably because the topLevel object has no "main" key.
{
"city":{ },
"cod":"200",
"message":0.1859,
"cnt":40,
"list":[ ]
}
Throw your JSON into here. https://jsonformatter.curiousconcept.com/
You'll notice that there are many, many "main" keys that are within the "list" element, but you have to parse those starting from getJSONArray("list").
Basically, something like this
String city = "undefined";
String country = "undefined";
List<Double> temperatures = new ArrayList<Double>();
try {
JSONObject object = new JSONObject(builder.toString());
JSONObject jCity = object.getJSONObject("city");
city = jCity.getString("name");
country = jCity.getString("country");
JSONArray weatherList = object.getJSONArray("list");
for (int i = 0; i < weatherList.length(); i++) {
JSONObject listObject = weatherList.getJSONObject(i);
double temp = listObject.getJSONObject("main").getDouble("temp");
temperatures.add(temp);
}
} catch (JSONException e) {
e.printStackTrace();
}
return new Wrapper(city, country, temperatures);
After studying your code, either your try block is failing, which is returning your object, but empty, or there is something wrong with your JSON parsing. If you could show us the JSON you are trying to parse that would be a great help.
That being said, the fact that it is still showing as "UNDEFINED" is because that is how you initialised it, and becuase (the JSON parse is likely failing), the object is being returned in an un-edited state.
EDIT:
You are parsing the JSON wrong. You are trying to find an object called "main" in the top directory, however the main object only exists inside of an array called list!
Please look here for a more easy to see and visual representation: http://prntscr.com/dlhlrk
You can use this site to help visualise your JSON and create an appropriate soluton based upon it. https://jsonformatter.curiousconcept.com/
Looking at the API you posted earlier (api.openweathermap.org) you are trying to access variables that don't exist. I suggest you have a look at what the API returns and try getting the variables one by one if you are getting a JSONException
EDIT:
What API you are using? In your initial post you said it was http://api.openweathermap.org/data/2.5/weather but in a comment above you said it was http://api.openweathermap.org/data/2.5/forecast.
If you're using the weather API (as initially stated) you can use the below:
#Override
protected Wrapper doInBackground(String... strings) {
Wrapper w = new Wrapper();
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String inputString;
while ((inputString = bufferedReader.readLine()) != null) {
builder.append(inputString);
}
Log.d("JSON", builder.toString());
JSONObject topLevel = new JSONObject(builder.toString());
JSONObject main = topLevel.getJSONObject("main");
JSONObject sys = topLevel.getJSONObject("sys");
w.Temperature = String.valueOf(main.getDouble("temp"));
w.city = topLevel.getString("name");
w.country = sys.getString("country");
urlConnection.disconnect();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return w;
}
I developing android app and now I have problem. Below is a part of my code, and it keeps skipping the "for" part. When I put a breakpoint inside for statement, it stops at the point, and executes the lines very well and makes an output that I want. When I just 'run' app, it skips that part so "String locations" value doesn't change. I googled and some say it's thread-related problem. So I put synchroinzed on the method, still not working. Any other suggestions?
UPDATE
I was trying to show code only related to the problem, but I think now showing the whole would be more useful for those who try to help so here's my entire code on showMapActivity. You can see I've tried some ways around and nothing worked. Saving path's information into String url is where I'm having problem. I tested, and other parts seem to work fine. I know my code is really massy, that was why I only posted parts of the code. TMap related classes are imported from .jar file.
public class showMapActivity extends Activity {
TMapData tmapdata=new TMapData();
TMapView tmapView;
TMapPoint origin, dest;
volatile ArrayList<TMapPoint> points=new ArrayList<>();
private TextView x;
private TextView y;
private HashMap<String,LatLng> coordinates;
private HashMap<LatLng,Double> finalpoint;
static private ConcurrentHashMap<Double,Double> path;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_map);
coordinates=new HashMap<>();
Intent intent=getIntent();
tmapView=new TMapView(this);
path=new ConcurrentHashMap<>();
coordinates=(HashMap<String,LatLng>)intent.getSerializableExtra("coordinate");
path=getPathPoints(coordinates);
int i=0;
String url=getUrl();
//String url = "https://maps.googleapis.com/maps/api/elevation/json?locations=";
//String locations="";
/*
Iterator<Double> keys= path.keySet().iterator();
while(keys.hasNext()){
Double key=keys.next();
//String lat=String.valueOf(key);
//String lng=String.valueOf(path.get(key));
locations=locations+String.valueOf(key)+","+String.valueOf(path.get(key));
if(keys.hasNext())
locations=locations+"|";
}path.entrySet()
*/
/*
for(ConcurrentHashMap.Entry<Double,Double> elem : path.entrySet())
{
String lat=String.valueOf(elem.getKey());
String lng=String.valueOf(elem.getValue());
locations=locations+lat+","+lng;
i++;
if(i!=path.size())
{
locations=locations+"|";
}
}
*/
//url=url+locations+"&key=AIzaSyDD88VFMPIfC5sr0XsFL0PDCE-QRN8gQto";
//String url=getUrl(path);
FetchUrl fetchUrl=new FetchUrl();
fetchUrl.execute(url);
}
private ConcurrentHashMap<Double,Double> getPathPoints(HashMap<String,LatLng> coordinates)
{
final ConcurrentHashMap<Double,Double> Path=new ConcurrentHashMap<>();
tmapView.setSKPMapApiKey("6bb5b7f3-1274-3c5e-ba93-790aee876673");
origin=new TMapPoint(coordinates.get("origin").latitude,coordinates.get("origin").longitude);
dest=new TMapPoint(coordinates.get("dest").latitude,coordinates.get("dest").longitude);
tmapdata.findPathData(origin, dest, new TMapData.FindPathDataListenerCallback() {
#Override
public void onFindPathData(TMapPolyLine polyLine) {
points=polyLine.getLinePoint();
for(TMapPoint point : points )
Path.put(point.getLatitude(),point.getLongitude());
}
});
return Path;
}
//ConcurrentHashMap<Double,Double> path
private synchronized String getUrl() {
int i=0;
String url = "https://maps.googleapis.com/maps/api/elevation/json?locations=";
String locations="";
for(HashMap.Entry<Double,Double> elem : path.entrySet())
{
String lat=String.valueOf(elem.getKey());
String lng=String.valueOf(elem.getValue());
locations=locations+lat+","+lng;
i++;
if(i!=path.size())
{
locations=locations+"|";
}
}
url=url+locations+"&key=AIzaSyDD88VFMPIfC5sr0XsFL0PDCE-QRN8gQto";
//https://maps.googleapis.com/maps/api/elevation/json?locations=
// 39.7391536,-104.9847034|36.455556,-116.866667&key=AIzaSyDD88VFMPIfC5sr0XsFL0PDCE-QRN8gQto
// Output format
return url;
}
private class FetchUrl extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... url) {
// For storing data from web service
String data = "";
try {
// Fetching the data from web service
//downloadURL
data = downloadUrl(url[0]);
Log.d("Background Task data", data.toString());
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//ParserTask
ParserTask parserTask = new ParserTask();
// Invokes the thread for parsing the JSON data
parserTask.execute(result);
}
}
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
//읽은 데이터를 버퍼에 저장
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
Log.d("downloadUrl", data.toString());
br.close();
} catch (Exception e) {
Log.d("Exception", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
private class ParserTask extends AsyncTask<String, Integer, ArrayList<Double>> {
// Parsing the data in non-ui thread
#Override
protected ArrayList<Double> doInBackground(String... jsonData) {
JSONObject jObject;
ArrayList<Double> altitude = null;
try {
jObject = new JSONObject(jsonData[0]);
Log.d("ParserTask",jsonData[0].toString());
//DataParser class 호출
DataParser parser = new DataParser();
Log.d("ParserTask", parser.toString());
// Starts parsing data
altitude = parser.parse(jObject);
Log.d("ParserTask","Getting Altitudes");
Log.d("ParserTask",altitude.toString());
} catch (Exception e) {
Log.d("ParserTask",e.toString());
e.printStackTrace();
}
return altitude;
}
// Executes in UI thread, after the parsing process
#Override
protected void onPostExecute(ArrayList<Double> result) {
finalpoint=new HashMap<>();
LatLng latLng;
int i=0;
for(HashMap.Entry<Double,Double> elem : path.entrySet() )
{
latLng=new LatLng(elem.getKey(),elem.getValue());
finalpoint.put(latLng,result.get(i++));
}
x = (TextView) findViewById(R.id.textView5);
y = (TextView) findViewById(R.id.textView6);
x.setText(String.valueOf(finalpoint.get(coordinates.get("origin"))));
y.setText(String.valueOf(finalpoint.get(coordinates.get("dest"))));
}
}
}
(Apologies for posting this as an answer - I don't yet have the required reputation to comment)
Simply adding synchronized to a method doesn't necessarily guarantee thread safety.
How and when is path being populated?
Update after additional information provided
The problem seems to be that the path points are being generated asynchronously, and you are trying to use them before the generation process has finished (or perhaps even started). This happens because the findPathData simply starts the generation process and returns immediately (i.e. before the generation process has finished). In your code, you then go on and build the URL which is supposed to contain the point data immediately. At this point the background point generation process may not have finished, and may not have even started. As a result the point map may be empty or incomplete, and your URL will not be generated as you expect.
You need to find a way to wait until all of the path point data has been returned by the asynchronous processing before creating the URL. This looks like it could be very difficult, if not impossible, with the version of the findPathData method you are using, because it returns points via the callback one at a time and you may not know how many will be generated.
I had a quick look at the API for TMapData and it has a findPathDataAll method which seems to generate all the points and return them in a single callback call rather than one by one. If this is indeed what it does (sorry, I can't read Korean), you could use this method and then generate the URL from the callback, because when it's called you know that the generation process has been completed. If you do this, be careful to make sure that you're on the main thread before interacting with the UI or Activity.
Hope that helps.
First time using Android Studio in any large capacity. Using the code from here: https://stackoverflow.com/a/30937657/5919360, I was able to successfully pull the information I wanted from the URL, but I can't figure out how use it.
Note: I know IMEI is not a good way to check for user registration and will be changing it later.
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
// Create instance and populates based on content view ID
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// store IMEI
String imei = tm.getDeviceId();
// store phone
String phone = tm.getLine1Number();
// Display IMEI - Testing Purposes Only
TextView imeiText = (TextView) findViewById(R.id.imeiDisplay);
imeiText.setText("IMEI:" + imei);
// Display phone number - Testing Purposes Only
TextView phoneText = (TextView) findViewById(R.id.phoneDisplay);
phoneText.setText("Phone:" + phone);
new DownloadTask().execute("http://www.url.com/mobileAPI.php?action=retrieve_user_info&IMEI="+imei);
}
private class DownloadTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
try {
return downloadContent(params[0]);
} catch (IOException e) {
return "Unable to retrieve data. URL may be invalid.";
}
}
#Override
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
}
private String downloadContent(String myurl) throws IOException {
InputStream is = null;
int length = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int response = conn.getResponseCode();
Log.d(TAG, "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = convertInputStreamToString(is, length);
return contentAsString;
} finally {
if (is != null) {
is.close();
}
}
}
public String convertInputStreamToString(InputStream stream, int length) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[length];
reader.read(buffer);
return new String(buffer);
}
}
This code returns an xml file, as a toast:
<?xml version="1.0" encoding="ISO-8859-1"?>
<mobile_user_info>
<rec>45</rec>
<IMEI>9900990099009</IMEI>
<fname>First</fname>
<lname>Last</lname>
<instance>instance1</instance>
<registered>N</registered>
</mobile_user_info>
I'm hoping someone can point me in the right direction for separating each line and using it independently. For example, if the Registered line comes back as N, a message is displayed like, 'You are not registered. Please contact administrator.'
Actually you should use an XML parser to parse the server's response. But if responses are always as simple as your example, you can use a regular expression to extract out the IMEI field.
String contentAsString = ...
Pattern pattern = Pattern.compile("<IMEI>(\d*)</IMEI>");
Matcher matcher = pattern.matcher(contentAsString);
if (matcher.find()) {
String imei = matcher.group(1);
}
I have a link where I'm providing 2 parameters and a php server side script is executing a query and writing them to my databae.
The problem is that in this specific case, it seems that I can't connect to teh url.
Here is my button xml file:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:includeFontPadding="#+id/btnSubmit"
android:text="#string/btnSubmit"
android:onClick="submitNewJ"
/>
Here is my click "listener":
public void submitNewJ(View view){
new submitJ().execute();
}
And here is the submitJ code:
public class submitJ extends AsyncTask<Void, Integer, Void>{
#Override
protected Void doInBackground(Void... params) {
try{
String encodedName = URLEncoder.encode(Name,"UTF-8");
String encodedBody = URLEncoder.encode(Body,"UTF-8");
URL url = new URL("http://site123.com/android/J/sJ.php?Name="+encodedName+"&Body="+encodedBody);
URLConnection urlConnection = url.openConnection();
#SuppressWarnings("unused")
InputStream in = urlConnection.getInputStream();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
And here is how I'm getting the strings:
EditText jN;
EditText jB;
String Name = "";
String Body = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_joke);
jN = (EditText)findViewById(R.id.newName);
jB = (EditText)findViewById(R.id.newBody);
Name = jN.getText().toString();
Body = jB.getText().toString();
}
It seems that the connection is not working here(even when I'm using the same code in otehr activities). Where am I mistaking?
I know that I'm missing something super small, but I'm not able to spot it.
P.s. My service and link are 100% tested and working.
The site you have in the example is probabbly for reference reasons, but mind that if it is using https protocol and you're reffering to a http, you will not get redirected to the correct link.
Everything in your code seems fine to me.
Just make sure that you're using the correct protocol.
You need to move these lines from onCreate() method to doInBackground() method,
Name = jN.getText().toString();
Body = jB.getText().toString();
put them inside the doInBackground() method like below,
public class submitJ extends AsyncTask<Void, Integer, Void>{
#Override
protected Void doInBackground(Void... params) {
try{
String Name = jN.getText().toString(); // move here and delete from the class
String Body = jB.getText().toString();
String encodedName = URLEncoder.encode(Name,"UTF-8");
String encodedBody = URLEncoder.encode(Body,"UTF-8");
URL url = new URL("http://site123.com/android/J/sJ.php?Name="+encodedName+"&Body="+encodedBody);
URLConnection urlConnection = url.openConnection();
#SuppressWarnings("unused")
InputStream in = urlConnection.getInputStream();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}