I am very new to Android, it would be great if you could help me with the error "Cannot resolve symbol ..." in the variables:
- value 1
- banner_id
- full_id
My code is the following:
package com.example.sienstranslation.siensapp;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.appdatasearch.GetRecentContextCall;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class Main3Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent=getIntent();
double precio = intent.getExtras().getDouble("precio");
String precio_rounded = String.format("%.2f", precio);
TextView txtCambio = (TextView) findViewById(R.id.textView4);
txtCambio.setText("Precio Total: "+ precio_rounded + " €");
}
void MakePostRequest() {
String posting_url ="http://ipaddress/app.php";
// its your url path ok
StringRequest postRequest = new StringRequest(Request.Method.POST, posting_url ,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
value1= jsonResponse.getString("precio_rounded");
} catch (JSONException e) {
e.printStackTrace();
banner_id = null;
full_id = null;
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
value1= null;
}
}
) {
// here is params will add to your url using post method
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("app", getString(R.string.app_name));
//params.put("2ndParamName","valueoF2ndParam");
return params;
}
};
Volley.newRequestQueue(this).add(postRequest);
}
Thank you very much for your time
At first , Declare banner_id & full_id ;
public class Main3Activity extends AppCompatActivity {
public int banner_id,full_id;
Then Clean-Rebuild Your Project .
It is a bad idea to start directly with Android ... when you don't have any clue about java itself. In your program, you are using those three variables without ever declaring them before. That is what the compiler is telling you. That is super basic stuff.
So I seriously recommend you to step back; and study those java basics for some more time. Otherwise your programming experience will be nothing else but a (probably short) series of very frustrating moments.
This is not meant to be rude; but it looks like you intend to build a skyscraper; but you actually have no idea how to dig the hole for the basement. That is simply not a very rewarding or efficient approach to get things done.
And you see, your follow-on comments to the answers ... just prove my point. Your current approach is nothing but trial-and-error. And just to be precise: the Android java programming model is itself something that can drive experienced java programmers nuts.
You must declare all variables before they can be used. The basic form of a variable declaration is shown here:
data type variable [ = value][, variable [= value] ...] ;
Related
I'm quite new to Java/Android Studio and am spending the past two days figuring this out, without succes. I keep getting: Cannot resolve constructor 'JsonArrayRequest(int, java.lang.String, java.lang.String, anonymous com.android.volley.Response.Listener)'
On other threads I see answers that suggest to replace the null to a string or cast the null but this doesn't seem to make the trick.
I'm trying to read the JSON Array at https://www.smartvibes.be/profiles/api/profileview.php?id=5073 for instance.
This is my code:
package com.smartvibes.smartbeat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class profileViewActivity extends AppCompatActivity {
RequestQueue rs;
String url,id, nick, age, city, mainpic, numpics, extrapic0, extrapic1, extrapic2, extrapic3, extrapic4, extrapic5;
TextView profileIntro;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_view);
Bundle getProfileId = getIntent().getExtras();
if(getProfileId == null){
return;
}
String profileid = getProfileId.getString("profileid");
url = "https://www.smartvibes.be/profiles/api/profileview.php?id="+profileid;
rs = Volley.newRequestQueue(this);
sendjsonrequest();
profileIntro = (TextView) findViewById(R.id.profileIntro);
//profileIntro.setText(profileData.getPnick());
profileIntro.setText(profileid);
}
public void sendjsonrequest(){
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, "", new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
// Get current json Array
JSONArray profile = new JSONArray(response);
JSONObject jresponse = profile.getJSONObject(0);
Toast.makeText(profileViewActivity.this, "test", Toast.LENGTH_LONG).show();
nick = jresponse.getString("nick");
age = jresponse.getString("age");
city = jresponse.getString("city");
mainpic = jresponse.getString("mainpic");
numpics = jresponse.getString("numpics");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
rs.add(jsonArrayRequest);
}
}
When trying to run it I get:
error: no suitable constructor found for JsonArrayRequest(int,String,String,<anonymous Listener<JSONArray>>)
constructor JsonArrayRequest.JsonArrayRequest(String,Listener<JSONArray>,ErrorListener) is not applicable
(actual and formal argument lists differ in length)
constructor JsonArrayRequest.JsonArrayRequest(int,String,JSONArray,Listener<JSONArray>,ErrorListener) is not applicable
(actual and formal argument lists differ in length)
I'm probably doing something (or several things) bad... But can't figure out what.
You miss Response.ErrorListener()
Use this
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
// Get current json Array
JSONArray profile = new JSONArray(response);
JSONObject jresponse = profile.getJSONObject(0);
Toast.makeText(profileViewActivity.this, "test", Toast.LENGTH_LONG).show();
nick = jresponse.getString("nick");
age = jresponse.getString("age");
city = jresponse.getString("city");
mainpic = jresponse.getString("mainpic");
numpics = jresponse.getString("numpics");
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
// Do something when error occurred
Snackbar.make(
mCLayout,
"Error...",
Snackbar.LENGTH_LONG
).show();
}
}
);
Hope it's help
Everything seems fine in your codes except ErrorListener.
If you check the source of the JsonArrayRequest, you'll notice that it takes another method as the last parameter which handles the errors which you don't have it.
Simply, adding the Response.ErrorListener() right after the onResponse() method will solve the issue.
You can however make it null to ignore errors, which is not recommended.
Take a look: https://android--examples.blogspot.com/2017/02/android-volley-json-array-request.html
My weather app uses Openweathermap API. But i can only get forecast for the city I put in the code.
I want to add an option so the user types his city name and get the weather forecast for it.
I've added an EditText to the layout but don't know how to get the data from it and use as the city name input.
Here is my code:
package com.hamed.myapplication;
import android.content.Context;
import android.util.Log;
import com.android.volley.DefaultRetryPolicy;
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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hamed.myapplication.view.dataModel.WeatherInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by Hamed on 8/20/2017.
*/
public class ApiService {
private static final String TAG = "ApiService";
private Context context;
public ApiService (Context context){
this.context=context;
}
public void getCurrentWeather(final OnWeatherInfoRecieved onWeatherInfoRecieved, String cityName){
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET,
"http://api.openweathermap.org/data/2.5/weather?q=ahvaz&apikey=01a477912e47daf2010808cc62015829",
null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.i(TAG, "onResponse: "+response.toString());
onWeatherInfoRecieved.onRecieved(parseResponseToWeatherInfo(response));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "onErrorResponse: "+error.toString());
onWeatherInfoRecieved.onRecieved(null);
}
});
request.setRetryPolicy(new DefaultRetryPolicy(8000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue= Volley.newRequestQueue(context);
requestQueue.add(request);
}
private WeatherInfo parseResponseToWeatherInfo(JSONObject response){
WeatherInfo weatherInfo= new WeatherInfo();
try {
JSONArray weatherJsonArray= response.getJSONArray("weather");
JSONObject weatherJsonObject= weatherJsonArray.getJSONObject(0);
weatherInfo.setWeatherName(weatherJsonObject.getString("main"));
weatherInfo.setWeatherName(weatherJsonObject.getString("description"));
JSONObject mainJsonObject=response.getJSONObject("main");
weatherInfo.setWeatherTemperature((float)mainJsonObject.getDouble("temp"));
weatherInfo.setHumidity(mainJsonObject.getInt("humidity"));
weatherInfo.setPressure(mainJsonObject.getInt("pressure"));
weatherInfo.setMinTemperature((float)mainJsonObject.getDouble("temp_min"));
weatherInfo.setMaxTemperature((float)mainJsonObject.getDouble("temp_max"));
JSONObject windJsonObject=response.getJSONObject("wind");
weatherInfo.setWindSpeed((float)windJsonObject.getDouble("speed"));
weatherInfo.setWindDegree((float)windJsonObject.getDouble("deg"));
return weatherInfo;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
public interface OnWeatherInfoRecieved {
void onRecieved(WeatherInfo weatherInfo);
}
}
Edit:
My app has a button to request data from server. I want the user to type his city name in the EditText and the API address use that city name in it...
Someone told me I have to use the EditText value as QueryString and give it to the API.
Does anyone know how to do this?
To get value from EditText:
EditText edit = (EditText) view.findViewById("myEditText");
String city = edit.getText().toString();
Is that what you're looking for?
To add city selection, you would have to call the remote openweathermap service and retrieve the cities.
For Openweathermap API, you can refer to this tutorial, which explains in detail how to add the city search option in your weather app. The final source code for it is on Github here. You can take a look in the XML file and the Java Class specifically for the city selection in the sample application source code.
I started to use Socketio, and I got a problem. I can't send a simple message to my flask session. My Java code:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException;
import io.socket.client.IO;
import io.socket.client.Socket;
public class MainActivity extends AppCompatActivity {
Socket socket;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
socket = IO.socket("http://192.168.8.101:8080/send");
} catch (URISyntaxException e) {
}
}
public void TestButton(View v){
Log.d("send","before send");
JSONObject obj = new JSONObject();
try {
obj.put("message", "hi");
obj.put("binary", new byte[42]);
socket.emit("mes", obj);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
My Python code:
from flask import Flask
from flask_socketio import SocketIO
app = Flask(__name__)
socketio = SocketIO(app)
#socketio.on("mes", namespace="/send")
def chat_message(message):
print("message:")
if __name__ == "__main__":
print("start")
socketio.run(app, host="0.0.0.0", port=8080)
The error:
TypeError: a bytes-like object is required, not 'str' <Greenlet at 0x24118773178: _handle_and_close_when_done(<bound method WSGIServer.handle of <WSGIServer at , <bound method StreamServer.do_close of <WSGIServer, (<gevent._socket3.socket [closed] object, fd=-1, )> failed with TypeError
I hope someone is able to help me with this problem. I've searched everywhere and i can't find my answer.
I think you use python3 with the gevent and gevent-websocket plugin.
gevent-websocket does not support python3 yet.
You have to options here:
Uninstall gevent and gevent-websocket and use eventlet.
Fix the code geventwebsocket/handler.py at line 236.
if b'101' not in self.status:
I hope I could help you.
I'm using Volley in an Android App to fetch data from the Misfit API (http://build.misfit.com). I tried to construct an intermittent activity, after someone logged in, to get all the data from the API. In that activity, I perform a JsonObject GET request, that should give me some information about the user of the app. Here's the code so far:
package com.iss_fitness.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
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.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import learn2crack.weboauth2.R;
public class LoadingScreenActivity extends Activity {
//Introduce an delay
private final int WAIT_TIME = 500;
private static final String QUERY_URL = "https://api.misfitwearables.com/move/resource/v1/user/me/profile";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("LoadingScreenActivity screen started");
setContentView(R.layout.loading_screen);
findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE);
// Instantiate the RequestQueue.
final RequestQueue queue = Volley.newRequestQueue(this);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
executeJson();
System.out.println("Going to Profile Data");
/* Create an Intent that will start the ProfileData-Activity. */
Intent mainIntent = new Intent(LoadingScreenActivity.this, DataView.class);
LoadingScreenActivity.this.startActivity(mainIntent);
LoadingScreenActivity.this.finish();
}
}, WAIT_TIME);
}
private Response.ErrorListener createRequestErrorListener() {
return new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);
}
};
}
private void executeJson() {
SharedPreferences prefs = this.getSharedPreferences("AppPref", MODE_PRIVATE);
final String token = prefs.getString("token", null);
RequestQueue queue = Volley.newRequestQueue(this);
Map<String, String> params = new HashMap<String, String>();
System.out.println(token);
params.put("access_token", token);
CustomRequest jsonRequest = new CustomRequest(Request.Method.GET, QUERY_URL, params,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
}, this.createRequestErrorListener());
System.out.println(jsonRequest);
queue.add(jsonRequest);
}
}
I'm quite new to Android development, so please bear with me, I'll try to describe the code. I've implemented a help class as suggested for JsonObjectRequest, as, as I understood, you can't override the getparams method when defining a request locally. the executeJson() method is the interesting one: I get the user access token from my SharedPreferences (Where it's correctly stored), put that in a String Map and give that to the CustomRequest, where, inside the help class, it gets thrown into a getparams method that simply returns the params. The responselistener sadly never gets called, as the errorlistener reports the following:
com.android.volley.NoConnectionError: java.io.IOException: No authentication challenges found
According to the API reference of Misfit, that should work.
Now, I know that a GET request requires "headers" and not "params" but does that make any difference?
Okay, I found a solution. The helper class contained an overriding getparams method, but no getheaders method. GET request requires getheaders, post requires getparams.
I tried to run an android project in Cordova with self-build plugin,
Heres the code in JS and java for the plugin
var cordova = require('cordova');
var Carrier = function() {};
Carrier.prototype.getCarrierCode = function(success, error) {
cordova.exec(success, error, 'CarrierPlugin', 'getCarrierCode', []);
};
var carrier = new Carrier();
module.exports = carrier;
this is the java code:
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.content.ContentResolver;
public class CarrierPlugin extends CordovaPlugin{
public static final String TAG = "CarrierPlugin";
public static final String ACTION_GET_CARRIER_CODE = "getCarrierCode";
//public TelephonyManager tm;
#Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
// TODO Auto-generated method stub
super.initialize(cordova, webView);
}
#Override
public boolean execute(String action, CordovaArgs args,
CallbackContext callbackContext) throws JSONException{
callbackContext.success("run it");
return true;
}
}
the error I got is "cannot find symbol :"
the strange thing is that, even if I change the code in the CarrierPlugin.java(delete the JSONException in line 16), it reported the same error.
My Eclipse says, you should import CordovaInterface, CordovaWebView and CordovaArgs.
And you might be missing package declaration as well, depending wherever you want to put your plugin's java files, such as:
package com.yourdomain.etc in the very first line of your Java file.