Note for volley and action process button


筆記一下最主要部份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package im.chair.app.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

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.dd.processbutton.iml.ActionProcessButton;

import org.json.JSONException;
import org.json.JSONObject;

import im.chair.app.R;

public class UserProfileActivity extends BaseActivity {

private static final String TAG = "UserProfileActivity";

EditText editTextAPIKey;
ActionProcessButton buttonFetch;
RequestQueue requestQueue;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.getActionBar().hide();
this.getActionBar().setDisplayUseLogoEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);

this.setContentView(R.layout.activity_profile);

this.requestQueue = Volley.newRequestQueue(this);

this.editTextAPIKey = (EditText) this.findViewById(R.id.editTextAPIKey);

this.buttonFetch = (ActionProcessButton) this.findViewById(R.id.buttonFetch);
this.buttonFetch.setMode(ActionProcessButton.Mode.ENDLESS);
this.buttonFetch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonFetch.setEnabled(false);
editTextAPIKey.setEnabled(false);
editTextPassword.setEnabled(false);

// Start progress animation (Start at 0 will not show animation)
Log.d(TAG, "---> Button set to progress");
buttonFetch.setProgress(1);

// Start request
Log.d(TAG, "---> Start connect api");
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("api_key", editTextAPIKey.getText());

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST,
"http://10.0.1.10:5000/api/user/1",
jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d(TAG, response.getString("username"));
buttonFetch.setProgress(100);
}catch(JSONException e) {
Log.d(TAG, "---> Response Listener");
Log.d(TAG, e.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "---> Fetch Profile Error");
Log.d(TAG, error.toString());
buttonFetch.setProgress(0);
}
}
);

requestQueue.add(jsonObjectRequest);
}catch(JSONException e) {
Log.d(TAG, "---> JSON Object");
Log.d(TAG, e.toString());
}
}
});
}

}