Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Post Json con Volley
package com.sviluppo.pierangelo.timesheet;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.apache.http.client.methods.HttpPost;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StartActivity extends AppCompatActivity
{
public static final String MY_USER_PREFERENCE = "MyPrefsFile";
public static final String myurl = "http://timesheet.web-dev.info/timesheets.json";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// richiamo SharedPreferences
SharedPreferences prefs = getSharedPreferences(MY_USER_PREFERENCE, MODE_PRIVATE);
String restoredUser = prefs.getString("mUsername", null);
TextView txtBenvenuto = (TextView) findViewById(R.id.labelBenvenuto);
txtBenvenuto.setText("Benvenuto " + restoredUser);
// ottengo data e ora attuali
String current_time = DateFormat.getDateTimeInstance().format(new Date());
Log.d("data", current_time);
TextView txtDate = (TextView) findViewById(R.id.labelDate);
txtDate.setText(current_time);
Button btnInizio = (Button) findViewById(R.id.btnInizio);
btnInizio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://timesheet.web-dev.info/timesheets.json";
try {
RequestQueue requestQueue = Volley.newRequestQueue(v.getContext());
// Prepares POST data...
JSONObject jsonBody = new JSONObject();
jsonBody.put("worker_id", "1");
final String mRequestBody = jsonBody.toString();
// Volley request...
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
mRequestBody, "utf-8");
return null;
}
}
};
requestQueue.add(request);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://...";
// Prepares POST data...
JSONObject jsonBody = new JSONObject();
jsonBody.put("TakeoffID", "2");
jsonBody.put("ViewPhoto1", "image base64 content");
jsonBody.put("ViewPhoto2", "image base64 content");
// "OrderLineid": "964"...
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("OrderLineid","964");
jsonObject1.put("OrderLinePhoto1","image base64 content");
jsonObject1.put("OrderLinePhoto2","image base64 content");
// "OrderLineid": "963"...
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("OrderLineid","963");
jsonObject2.put("OrderLinePhoto1","image base64 content");
jsonObject2.put("OrderLinePhoto2","image base64 content");
JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonObject1);
jsonArray.put(jsonObject2);
// "LineItems"...
jsonBody.put("LineItems", jsonArray);
final String mRequestBody = jsonBody.toString();
// Volley request...
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
mRequestBody, "utf-8");
return null;
}
}
};
requestQueue.add(request);
} catch (JSONException e) {
e.printStackTrace();
}
Hope it helps!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.