Created
June 17, 2020 01:12
-
-
Save godjooyoung/38fa778aa2e2f4852eeb9ff5f20b381a to your computer and use it in GitHub Desktop.
안드로이드 리퀘스트
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.volley; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import androidx.appcompat.app.AppCompatActivity; | |
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.toolbox.StringRequest; | |
import com.google.gson.Gson; | |
import java.util.HashMap; | |
import java.util.Map; | |
import static com.android.volley.toolbox.Volley.newRequestQueue; | |
public class Volley extends AppCompatActivity { | |
TextView tv; | |
EditText et; | |
Button bt; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_volley); | |
tv=findViewById(R.id.textview); | |
et=findViewById(R.id.tempId); | |
bt=findViewById(R.id.button); | |
bt.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
makeRequest(); | |
} | |
}); | |
}//end on Create | |
private void makeRequest(){ | |
//이 메서드를 onCreate 안에서 호출한다. | |
//리퀘스트를 담을 큐를 만든다. | |
RequestQueue queue = newRequestQueue(this); | |
String url = "http://192.168.0.84:90/prj/logintest.do"; | |
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
Gson gson = new Gson(); | |
HashMap<String, String> map = gson.fromJson(response, HashMap.class); | |
tv.setText(map.get("result")+ map.get("id") + "성공쓰"); | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
tv.setText(error + "눈물이 주루룩"); | |
} | |
}){ | |
@Override | |
protected Map<String, String> getParams() throws AuthFailureError { | |
Map<String, String> params = new HashMap<String, String>(); | |
params.put("id", et.getText().toString()); | |
return params; | |
} | |
}; | |
queue.add(stringRequest); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment