Reputation: 335
I am currently trying to post and get a JSON from alchemy, I already get the get, howver, I cannot post, does anyone know the correct format to psot to alchemy from an edittext in an android app? Does anyone know, I really need to know what the call is for the post?
public class MainActivity extends AppCompatActivity {
private VolleySingleton mVolleySingleton;
private RequestQueue mRequestQueue;
private RequestQueue mrequestQueue;
private static final String URL_GET="URL";;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context getContext=(MainActivity.this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
Button SunnyButton = (Button) findViewById(R.id.PostButton);
setSupportActionBar(toolbar);
mVolleySingleton = VolleySingleton.getInstance();
mRequestQueue = mVolleySingleton.getRequestQueue();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL_GET, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void ClickMe(View view) {
final EditText editMe=(EditText)findViewById(R.id.EditText);
String readText = editMe.getText() + " ";
if (readText.length() == 1) {
Toast toast = Toast.makeText(getApplicationContext(),
"Please Enter Text", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
return;
}
mVolleySingleton = VolleySingleton.getInstance();
mrequestQueue = mVolleySingleton.getRequestQueue();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL_GET, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response + "reponse");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
System.out.println("************" + error + "error");
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("text", editMe.getText().toString());
System.out.println(parameters + "parameters");
return parameters;
}
};
System.out.println("request " + request);
mrequestQueue.add(request);
}
}
Upvotes: 3
Views: 335
Reputation: 522
It depends on what you are posting to Alchemy, and what you are trying to do. You should probably take at look at and familiarize yourself with the Alchemy API documentation (http://www.alchemyapi.com/api/calling-the-api). Make sure that you have an Alchemy API key, and that you are aware of the call limitiations imposed by your particular billing plan.
Upvotes: 1