gpt4 book ai didi

Android volley 如何接收和发送一个 json

转载 作者:行者123 更新时间:2023-11-29 19:42:17 24 4
gpt4 key购买 nike

我制作了一个应用程序,在特定部分我发送了一个字符串并接收了一个 json。我用 Volley 效果很好,但现在我需要发送一个 json。

这是我的代码:

 public static final String DATA_URL = "http://unynote.esy.es/cas/read_allorder.php?id=";  // THIS HAVE TO CHANGE JUST TO LOCALHOST:8080/LOGIN

这里:

public class Config {
public static final String DATA_URL = "http://unynote.esy.es/cas/read_allorder.php?id="; // THIS HAVE TO CHANGE JUST TO LOCALHOST:8080/LOGIN
public static final String KEY_NAME = "COD_ALUMNO";
public static final String KEY_ADDRESS = "COD_ASIGNATURA";
public static final String KEY_VC = "GRUPO_SECCION";

public static final String KEY_AULA = "AULA";
public static final String KEY_DIA = "DIA";
public static final String KEY_INICIO = "INICIO";
public static final String KEY_FIN = "FIN";

public static final String JSON_ARRAY = "result";

}

这是 VOLLEY 代码的一部分

public class TabsActivity extends AppCompatActivity implements  
View.OnClickListener {

private EditText editTextId;
private Button buttonGet;
private TextView textViewResult;

private ProgressDialog loading;

int cont=1;
String[ ] contenido = new String[7];
String f="";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainint);

editTextId = (EditText) findViewById(R.id.editTextId);
buttonGet = (Button) findViewById(R.id.buttonGet);
textViewResult = (TextView) findViewById(R.id.textViewResult);

buttonGet.setOnClickListener(this);
}

private void getData() {
String id = editTextId.getText().toString().trim();
if (id.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

String url = Config.DATA_URL+editTextId.getText().toString().trim();

StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
Toast.makeText(getBaseContext(), "si", Toast.LENGTH_LONG).show();

showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(TabsActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}

private void showJSON(String response){

// Toast.makeText(getBaseContext(), response, Toast.LENGTH_LONG).show();

String name="";
String address="";
String grupo = "";
String aula = "";
String dia = "";
String inicio = "";
String fin = "";



try {
Toast.makeText(getBaseContext(), "LOGIN... ", Toast.LENGTH_LONG).show();
JSONObject jsonObject = new JSONObject(response);

JSONArray ja = jsonObject.getJSONArray("orders");
// JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
for (int i = 0; i < ja.length(); i++) {
JSONObject collegeData = ja.getJSONObject(i);
name = collegeData.getString("id");
address = collegeData.getString("item");
grupo = collegeData.getString("GRUPO_SECCION");
aula = collegeData.getString("AULA");
dia = collegeData.getString("DIA");
inicio = collegeData.getString("INICIO");
fin = collegeData.getString("FIN");

///database
DBAdapter db= new DBAdapter(this);

db.open();
long id = db.insertContact(address, aula,dia,inicio,fin );
db.close();

db.open();
Cursor c = db.getAllContacts();
if (c.moveToFirst())
{ do{
contenido=getcontenido(c);

}while (c.moveToNext());
}
db.close();
cont= Integer.parseInt( contenido[0]);
/// database

/// alarms
int [] time;
time = parsetime(inicio);

int horai = time[0];
int minutoi = time[1];
int diaa = getDay(dia);

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, horai);
cal.set(Calendar.MINUTE, minutoi);
cal.set(Calendar.DAY_OF_WEEK, diaa);
cal.add(Calendar.SECOND, 2);


Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("name", address);
//intent.putExtra("curos bn",1);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getBaseContext(),
cont+1, intent, PendingIntent.FLAG_UPDATE_CURRENT );




AlarmManager alarmManager =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 24 * 7 * 60 * 60 * 1000 , pendingIntent);


////alarms



f=f+"codigo alumno:\t"+name+"\ncodigo/nombre curso:\t" +address+ "\ngrupo:\t"+grupo+"\naula:\t"
+aula+"\ndia:\t"+dia+"\ninicio:\t"+inicio+"\nfin:\t"+fin+"\n:\t";

}
// Toast.makeText(getBaseContext(), collegeData.length(), Toast.LENGTH_LONG).show();
//collegeData.toString();
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText(f);
}

我刚刚发送了字符串 editTextId.getText() 。这是每个用户的代码,但现在我需要发送一个包含该字符串的 json。

'CCODUSU' '45875621'

CCODUSU是标识符

最佳答案

我会看一下 StringRequests。这是一个如何将内容发送到 PHP 文件的示例,该文件更新数据库,或者可以执行任何操作:

SetMyStuff.java:

package com.example.your_app.database_requests;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class SetMyStuff extends StringRequest {

private static final String LOGIN_REQUEST_URL = "http://example.com/SetMyStuff.php";
private Map<String, String> params;

public SetMyStuff(String username, String password, Response.Listener<String> listener) {
super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("username", username);
params.put("password", password);
}

@Override
public Map<String, String> getParams() {
return params;
}

}

要调用此 StringRequest:

Response.Listener<String> listener = new Response.Listener<String>() {

@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);

boolean success = jsonResponse.getBoolean("success");

if (!success) {
Log.e(TAG, "Could not update stuff.");
} else {
Log.e(TAG, "Updated stuff.");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
SetMyStuff setMyStuffRequest = new SetMyStuff(username, password, listener);
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(setMyStuffRequest);

接收此信息的 PHP 文件:

<?php

$password = $_POST["password"];
$username = $_POST["username"];

$con = mysqli_connect("website.com", "dbusername", "dbpassword", "dbtable");

$response = array();
$response["success"] = false;

/* Do something */
$response["success"] = true;

echo json_encode($response);
?>

关于Android volley 如何接收和发送一个 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38471006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com