Andriod volley, request restful
******* volley localhost not work, because call simulator localhost, it need to change to use IIS and local IP
1. Add dependencies
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.android.volley:volley:1.1.1'
}
2. Test code
public void sendMessage(View view){
//Test call api
//https://localhost:44355/api/driversRestful/1
RequestQueue queue = Volley.newRequestQueue(this);
// String url ="http://localhost:54066/api/driversRestful/1";
String url = "http://192.168.0.165:8080/api/driversRestful/1";
// Request a string response from the provided URL.
// Request a string response from the provided URL
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
//textView.setText("Response is: "+ response.substring(0,500));
Log.i(TAG, response);
//Log.i(TAG, response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//textView.setText("That didn't work!");
Log.i(TAG, error.toString());
}
});
queue.add(stringRequest);
/* Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
*/
}
Referance
http://tw-hkt.blogspot.com/2020/03/android-volley-java.html
Error
com.android.volley.NoConnectionError: java.io.IOException: Cleartext HTTP traffic to localhost not permitted
solution:
it works
localhost not work due to run simulator localhost, i change to use iis
留言
張貼留言