how to use progressbar in volly multipart request to upload images. i am using VolleyPlus library for multipart request. my multipart request code is
SimpleMultiPartRequest smr = new SimpleMultiPartRequest(Request.Method.POST, BASE_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("Response", response);
try {
JSONObject jObj = new JSONObject(response);
String message = jObj.getString("message");
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
smr.addFile("image", imagePath);
smr.addMultipartParam("name", fileType, fileName);
MyApplication.getInstance().addToRequestQueue(smr);
now i want to add progressbar for uploading file like this:
or
Related
I want to get json response from local server. I want to add port in that. how to add port while using volley in android
In main activity
{
String url = "192.2.3.1:80/data";
sendAndRequestResponse(url, new VolleyCallback(){
#Override
public void onSuccess(JSONObject result){
}
});
}
private void sendAndRequestResponse(final String url, final VolleyCallback callback) {
//RequestQueue initialized
mRequestQueue = Volley.newRequestQueue(MainActivity.this, new ProxyPort());
//String Request initialized
mStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), "Response :" + response.toString(), Toast.LENGTH_LONG).show();//display the response on screen
Log.e("url", url);
Log.e("Response", response.toString());
try {
JSONObject obj = new JSONObject(response);
callback.onSuccess(obj);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i("ERR", "Error :" + error.toString());
}
});
VolleyLog.DEBUG = true;
mRequestQueue.add(mStringRequest);
}
I want to add port in this url. I want to access local host using ip. How to achieve it
I'm getting a Null Exception with JSon object from Volley
I filled the JSon object before it is used, and not in the parameterlist.
public static void SendPost6(final Context context){
final String TAG= "-->Error-->";
String url = "http://192.168.44.120/test_php_neuer_user.php";
RequestQueue queue = Volley.newRequestQueue(context);
JSONObject userObject = new JSONObject();
JSONObject paramsObject = new JSONObject();
try {
paramsObject.put("name", "Name");
paramsObject.put("email", "EMail");
userObject.put("user",paramsObject);
}
catch (JSONException e){
Toast.makeText(context, "JSON-Error:" + e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,
userObject,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(context, "Volley Response:" + response.toString(), Toast.LENGTH_LONG).show();
}},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//handle errors#
Log.d(TAG, "Failed with error msg:\t" + error.getMessage());
Log.d(TAG, "Error StackTrace: \t" + error.getStackTrace());
Toast.makeText(context, "Volley Error:" + error.getMessage(), Toast.LENGTH_LONG).show();
error.printStackTrace();
try {
byte[] htmlBodyBytes = error.networkResponse.data;
Log.e(TAG, new String(htmlBodyBytes), error);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
queue.add(request);
//AppController.getInstance().addToRequestQueue(request);
I want to add the json object request into the queue to perform http-post. Volley Error is "null"
The firewall blocked the network access. This is why i'm getting response null. Solved.
please anyone help me the list size in my code return Zero
note:- list is global variable when get the size is return zero
public void getdata(String cities) {
String url = "http://api.openweathermap.org/data/2.5/weather?q=" + cities + "&APPID=8072ed7ede0fc9bcb8591a2a2eb90cfd";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
CityWeather cityWeather = new CityWeather();
JSONObject object = new JSONObject(response);
cityWeather.setLonitude(String.valueOf(object.getJSONObject("coord").getDouble("lon")));
cityWeather.setLatitude(String.valueOf(object.getJSONObject("coord").getDouble("lat")));
cityWeather.setCityName(object.getString("name"));
cityWeather.setCurrentTemp(String.valueOf(object.getJSONObject("main").getDouble("temp")));
cityWeather.setPressure(String.valueOf(object.getJSONObject("main").getInt("pressure")));
cityWeather.setHumidity(String.valueOf(object.getJSONObject("main").getInt("humidity")));
cityWeather.setMinTemp(String.valueOf(object.getJSONObject("main").getDouble("temp_min")));
cityWeather.setMaxTemp(String.valueOf(object.getJSONObject("main").getDouble("temp_max")));
cityWeather.setWindSpeed(String.valueOf(object.getJSONObject("wind").getDouble("speed")));
cityWeather.setWindDegree(String.valueOf(object.getJSONObject("wind").getInt("deg")));
list.add(cityWeather);
} catch (JSONException e) {
Toast.makeText(MainActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}
Thanks for all
Volley is not synchronous, it takes the request and give you the callback when the request is completed. If you check the size after onResponse is called you will get the desired result
String url = "http://api.openweathermap.org/data/2.5/weather?q=" + cities + "&APPID=8072ed7ede0fc9bcb8591a2a2eb90cfd";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
CityWeather cityWeather = new CityWeather();
JSONObject object = new JSONObject(response);
cityWeather.setLonitude(String.valueOf(object.getJSONObject("coord").getDouble("lon")));
cityWeather.setLatitude(String.valueOf(object.getJSONObject("coord").getDouble("lat")));
cityWeather.setCityName(object.getString("name"));
cityWeather.setCurrentTemp(String.valueOf(object.getJSONObject("main").getDouble("temp")));
cityWeather.setPressure(String.valueOf(object.getJSONObject("main").getInt("pressure")));
cityWeather.setHumidity(String.valueOf(object.getJSONObject("main").getInt("humidity")));
cityWeather.setMinTemp(String.valueOf(object.getJSONObject("main").getDouble("temp_min")));
cityWeather.setMaxTemp(String.valueOf(object.getJSONObject("main").getDouble("temp_max")));
cityWeather.setWindSpeed(String.valueOf(object.getJSONObject("wind").getDouble("speed")));
cityWeather.setWindDegree(String.valueOf(object.getJSONObject("wind").getInt("deg")));
list.add(cityWeather);
//Check list size here and do whatever you want with the list
} catch (JSONException e) {
Toast.makeText(MainActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
I am using volley library in my project to connect database, but i am facing an issue.
When i use volley library in my code to post data to web getting wrong response always , but when i test in postman getting correct response..
i can't solve this issue. anyone can help me...
i am post my code and postman response here
Volley request
StringRequest request = new StringRequest(Request.Method.POST, REGISTER_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
Log.e("response", response);
try {
JSONObject jsonObject = new JSONObject(response);
Toast.makeText(getApplicationContext(), jsonObject.getString("error_code"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "json exception", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "error " + error.getMessage(), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> data = new HashMap<>();
data.put("username", name);
data.put("email", email);
data.put("password", password);
return data;
}
};
RequestQueue queue = Volley.newRequestQueue(SignUpActivity.this);
queue.getCache().clear();
//queue.getCache().remove(REGISTER_URL);
//request.setShouldCache(false);
queue.add(request);
Response when use volley
{"error_code":"1","error_message":"Email already exist !"}
Response from postman
{"error_code":"0","error_message":" "}
I am Trying to upload Images using volley library, I dont know How exactly.
I have referred previously asked questions, but nothing seems to satisfy.
Here is what i needed to acheive :
Below is the Code I tried to use :
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("**********", " Response Received is " + response);
try {
JSONObject jsonObject = new JSONObject(response);
Log.d("**********", "STRING TO JSON CONVERSION DONE , IT IS " + jsonObject.toString());
ParseReqOtp parseReqOtp = new ParseReqOtp();
parseReqOtp.parseImageupload(jsonObject);
} catch (Exception e) {
Log.d("**********", "ERROR IN STRING TO JSON CONVERSION " + e.toString());
}
Log.d("**********", "FETCHING IN VOLLEY REQ" + response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(AddPic.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected HashMap<String,String> getParams() {
HashMap<String, String> params = new HashMap<String, String>();
params.put("id", BaseActivity.getBaseActivity().getEstablishment_id());
params.put("type", "photos");
params.put("example_file",file.toString());
}
};
private void uploadImageWithStringParamsToServer() {
SimpleMultiPartRequest smr = new SimpleMultiPartRequest(Request.Method.POST, "http://139.59.16.103/addImage",
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("Response", response);
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
smr.addStringParam("id", "821470");
smr.addStringParam("type", "photos");
smr.addFile("example_file", "2014-11-17 17.08.33-2.jpg");
smr.setFixedStreamingMode(true);
smr.setOnProgressListener(this);
RequestQueue mRequestQueue = Volley.newRequestQueue(this);
mRequestQueue.add(smr);
mRequestQueue.start();
}