我的后端是laravel,我想将json文件发送到特定的溃败 我已经创建了我的json plz帮助
public class MainActivity extends AppCompatActivity {
EditText usernameview;
EditText passwordview;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameview = (EditText)findViewById(R.id.username) ;
passwordview = (EditText)findViewById(R.id.password) ;
Button login = (Button) findViewById(R.id.loginid);
}
public void senddatatoserver(View v) {
String username= usernameview.getText().toString();
String password = passwordview.getText().toString();
JSONObject login = new JSONObject();
try {
login.put("username",username);
login.put("password",password);
} catch (JSONException e) {
e.printStackTrace();
}
if (login.length() > 0) {
new SendDataToServer().execute(String.valueOf(login));
}
}
这是我发送数据的类我只想知道我应该在doinbackground方法中写什么
class SendDataToServer extends AsyncTask<String,String,String> {
@Override
protected String doInBackground(String... params) {
}
@Override
protected void onPostExecute(String s) {
}
}
答案 0 :(得分:1)
您可以使用排球发送请求
StringRequest stringRequest = new StringRequest(Request.Method.POST, YOUR_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
for (int i = 0; i < response.length(); i++) {
JSONObject json; // convert String to JSONObject
try {
json = new JSONObject(response);
JSONArray jsonArray = json.getJSONArray("data");
lyric_string = jsonArray.getJSONObject(0).getString("song_lyric");
artist_string = jsonArray.getJSONObject(0).getString("song_artist");
//album_string = jsonArray.getJSONObject(0).getString("song_album");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//error message
dismissDialog();
lyric.setText("Sorry No Lyric Found");
lyric.setVisibility(View.VISIBLE);
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("song_name", "A song Name");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);