DSRFINANCIAL
Lurker
I am very new to android coding and I am having a few issues that I cannot resolve. This class is the only one with errors, but I can't move forward until I can compile. Here are the issues. I hope I have provided enough info. If not, I can find out what you may need - just let me know.
1. The HttpClient has been removed for build 23, but the tutorial I am following had me write everything with HttpClient. How can I change to the new UrlConnection? I made some changes to the build.gradle and app.gradle but it will not compile the build. Most of the errors are due to this. All of the import org.apache... files are now showing up, except UrlEncodedFormEntity.
2. There are some expected semicolons, but it doesn't seem t make sense to me why it would be expected. I highlighted those few areas below.
Code:
package com.brasiltradefx.ctfxalerts;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import java.util.ArrayList;
/**
* Created by Deryk on 11/7/2015.
*/
public class ServerRequests {
ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 15000;
public static final String SERVER_ADDRESS = "removed";
public ServerRequests(Context context)
{
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing");
progressDialog.setMessage("Please wait..");
}
public void storeDataInBackground(Contact contact , GetUserCallback callback)
{
progressDialog.show();
new StoreDataAsyncTask(contact, callback).execute();
}
public void fetchDataInBackground(Contact contact , GetUserCallback callback)
{
progressDialog.show();
new fetchDataAsyncTask(contact, callback).execute();
}
public class StoreDataAsyncTask extends AsyncTask<Void , Void , Void> {
Contact contact;
GetUserCallback callback;
public StoreDataAsyncTask(Contact contact, GetUserCallback callback) {
this.contact = contact;
this.callback = callback;
}
@override
protected Void doInBackground(Void... voids) {
ArrayList<NameValuePair> data_to_send = new ArrayList<>();
data_to_send.add(new BasicNameValuePair("Firstname", contact.firstname));
data_to_send.add(new BasicNameValuePair("Lastname", contact.lastname));
data_to_send.add(new BasicNameValuePair("Email", contact.email));
data_to_send.add(new BasicNameValuePair("Username", contact.username));
data_to_send.add(new BasicNameValuePair("Password", contact.password));
data_to_send.add(new BasicNameValuePair("Phone", contact.phone));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
{
@override [//error - Annotations not allowed here]
public HttpResponse execute(HttpPost post); [//error - missing ";", expects ")", cannot resolve symbol post]
{
return null;
}
} ;
HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");
try {
post.setEntity(new UrlEncodedFromEntity(data_to_send)); [//error - cannot resolve symbol UrlEncodedFromEntity]
client.execute(post);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
callback.done(null);
super.onPostExecute(aVoid);
}
public class FetchDataAsyncTask extends AsyncTask<Void, Void, Contact> {
Contact contact;
GetUserCallback callback;
public FetchDataAsyncTask(Contact contact, GetUserCallback callback) {
this.contact = contact;
this.callback = callback;
}
@override
protected Contact doInBackground(Void... voids) {
ArrayList<NameValuePair> data_to_send = new ArrayList<>();
data_to_send.add(new BasicNameValuePair("Firstname", contact.firstname));
data_to_send.add(new BasicNameValuePair("Lastname", contact.lastname));
data_to_send.add(new BasicNameValuePair("Email", contact.email));
data_to_send.add(new BasicNameValuePair("Username", contact.username));
data_to_send.add(new BasicNameValuePair("Password", contact.password));
data_to_send.add(new BasicNameValuePair("Phone", contact.phone));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");
Contact returnedContact = null;
try {
post.setEntity(new UrlEncodedFromEntity(data_to_send)); [//error - cannot resolve symbol UrlEncodedFromEntity]
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(result);
returnedContact = null;
if (jsonObject.length() == 0) {
returnedContact = null;
} else {
String firstname, lastname, email, phone;
firstname = null;
lastname = null;
email = null;
phone = null;
if (jsonObject.has("firstname"))
firstname = jsonObject.getString("firstname");
if (jsonObject.has("lastname"))
lastname = jsonObject.getString("lastname");
if (jsonObject.has("email"))
email = jsonObject.getString("email");
if (jsonObject.has("phone"))
phone = jsonObject.getString("phone");
returnedContact = new Contact(firstname, lastname, email, contact.username, contact.password, phone);
}
} catch (Exception e) {
e.printStackTrace();
}
return returnedContact;
}
@override
protected void onPostExecute(Contact returnedContact) {
progressDialog.dismiss();
callback.done(returnedContact);
super.onPostExecute(returnedContact);
}
}
}
private class fetchDataAsyncTask {
public fetchDataAsyncTask(Contact contact, GetUserCallback callback) {
}
public void execute() {
}
}
}
1. The HttpClient has been removed for build 23, but the tutorial I am following had me write everything with HttpClient. How can I change to the new UrlConnection? I made some changes to the build.gradle and app.gradle but it will not compile the build. Most of the errors are due to this. All of the import org.apache... files are now showing up, except UrlEncodedFormEntity.
2. There are some expected semicolons, but it doesn't seem t make sense to me why it would be expected. I highlighted those few areas below.
Code:
package com.brasiltradefx.ctfxalerts;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import java.util.ArrayList;
/**
* Created by Deryk on 11/7/2015.
*/
public class ServerRequests {
ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 15000;
public static final String SERVER_ADDRESS = "removed";
public ServerRequests(Context context)
{
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing");
progressDialog.setMessage("Please wait..");
}
public void storeDataInBackground(Contact contact , GetUserCallback callback)
{
progressDialog.show();
new StoreDataAsyncTask(contact, callback).execute();
}
public void fetchDataInBackground(Contact contact , GetUserCallback callback)
{
progressDialog.show();
new fetchDataAsyncTask(contact, callback).execute();
}
public class StoreDataAsyncTask extends AsyncTask<Void , Void , Void> {
Contact contact;
GetUserCallback callback;
public StoreDataAsyncTask(Contact contact, GetUserCallback callback) {
this.contact = contact;
this.callback = callback;
}
@override
protected Void doInBackground(Void... voids) {
ArrayList<NameValuePair> data_to_send = new ArrayList<>();
data_to_send.add(new BasicNameValuePair("Firstname", contact.firstname));
data_to_send.add(new BasicNameValuePair("Lastname", contact.lastname));
data_to_send.add(new BasicNameValuePair("Email", contact.email));
data_to_send.add(new BasicNameValuePair("Username", contact.username));
data_to_send.add(new BasicNameValuePair("Password", contact.password));
data_to_send.add(new BasicNameValuePair("Phone", contact.phone));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
{
@override [//error - Annotations not allowed here]
public HttpResponse execute(HttpPost post); [//error - missing ";", expects ")", cannot resolve symbol post]
{
return null;
}
} ;
HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");
try {
post.setEntity(new UrlEncodedFromEntity(data_to_send)); [//error - cannot resolve symbol UrlEncodedFromEntity]
client.execute(post);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
callback.done(null);
super.onPostExecute(aVoid);
}
public class FetchDataAsyncTask extends AsyncTask<Void, Void, Contact> {
Contact contact;
GetUserCallback callback;
public FetchDataAsyncTask(Contact contact, GetUserCallback callback) {
this.contact = contact;
this.callback = callback;
}
@override
protected Contact doInBackground(Void... voids) {
ArrayList<NameValuePair> data_to_send = new ArrayList<>();
data_to_send.add(new BasicNameValuePair("Firstname", contact.firstname));
data_to_send.add(new BasicNameValuePair("Lastname", contact.lastname));
data_to_send.add(new BasicNameValuePair("Email", contact.email));
data_to_send.add(new BasicNameValuePair("Username", contact.username));
data_to_send.add(new BasicNameValuePair("Password", contact.password));
data_to_send.add(new BasicNameValuePair("Phone", contact.phone));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");
Contact returnedContact = null;
try {
post.setEntity(new UrlEncodedFromEntity(data_to_send)); [//error - cannot resolve symbol UrlEncodedFromEntity]
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(result);
returnedContact = null;
if (jsonObject.length() == 0) {
returnedContact = null;
} else {
String firstname, lastname, email, phone;
firstname = null;
lastname = null;
email = null;
phone = null;
if (jsonObject.has("firstname"))
firstname = jsonObject.getString("firstname");
if (jsonObject.has("lastname"))
lastname = jsonObject.getString("lastname");
if (jsonObject.has("email"))
email = jsonObject.getString("email");
if (jsonObject.has("phone"))
phone = jsonObject.getString("phone");
returnedContact = new Contact(firstname, lastname, email, contact.username, contact.password, phone);
}
} catch (Exception e) {
e.printStackTrace();
}
return returnedContact;
}
@override
protected void onPostExecute(Contact returnedContact) {
progressDialog.dismiss();
callback.done(returnedContact);
super.onPostExecute(returnedContact);
}
}
}
private class fetchDataAsyncTask {
public fetchDataAsyncTask(Contact contact, GetUserCallback callback) {
}
public void execute() {
}
}
}