Php 当响应只是表中的一行时,如何使用okhttp从json响应中获取java对象
我试图从json响应中获取java对象“Product”,json响应只返回表中的一行,我不知道是否必须使用JSONObject或JSONArray或两者兼用,无论如何,我尝试了两者,但都不起作用,它给了我一个NullPointerException。 这是我的密码:Php 当响应只是表中的一行时,如何使用okhttp从json响应中获取java对象,php,android,json,okhttp3,Php,Android,Json,Okhttp3,我试图从json响应中获取java对象“Product”,json响应只返回表中的一行,我不知道是否必须使用JSONObject或JSONArray或两者兼用,无论如何,我尝试了两者,但都不起作用,它给了我一个NullPointerException。 这是我的密码: public class ConsultProductActivity extends AppCompatActivity { String idProduit; Product prod; @Over
public class ConsultProductActivity extends AppCompatActivity {
String idProduit;
Product prod;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extra = intent.getExtras();
idProduit = extra.getString("idProduit");
Toast.makeText(ConsultProductActivity.this, idProduit, Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_consult_product);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_rest);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
prod = prepareFullInfo();
getSupportActionBar().setTitle(prod.getName());
ImageView imgAvant = (ImageView) findViewById(R.id.img_avant);
Glide.with(ConsultProductActivity.this).load(prod.getImg_avant()).into(imgAvant);
ImageView imgArriere = (ImageView) findViewById(R.id.img_arriere);
Glide.with(ConsultProductActivity.this).load(prod.getImg_arriere()).into(imgArriere);
ImageView imgCote = (ImageView) findViewById(R.id.img_cote);
Glide.with(ConsultProductActivity.this).load(prod.getImg_coté()).into(imgCote);
TextView prix = (TextView) findViewById(R.id.prix_consult_p);
prix.setText(prod.getPrice());
AppCompatTextView description = (AppCompatTextView) findViewById(R.id.description);
description.setText(prod.getDescription());
}
public Product prepareFullInfo() {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
String productUrl = new String("http://moodyinformatics.000webhostapp.com/product_full_info.php?id=" + idProduit);
@Override
protected Void doInBackground(Void... params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(productUrl).build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
prod = new Product(object.getInt("id_produit"), object.getString("nom"), object.getString("description"), object.getInt("qte_stock"), object.getInt("prix"), object.getString("img_avant"), object.getString("img_arriere"), object.getString("img_cote"));
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
}
};
task.execute();
return prod;
}
AsyncTask在新线程上工作以执行特定任务。当任务已在执行时,方法PrepareFileInfo立即返回。您必须在onPostExecute方法中获得结果
public class ConsultProductActivity extends AppCompatActivity {
String idProduit;
Product prod;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extra = intent.getExtras();
idProduit = extra.getString("idProduit");
Toast.makeText(ConsultProductActivity.this, idProduit, Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_consult_product);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_rest);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
prepareFullInfo();
}
public void prepareFullInfo() {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
String productUrl = new String("http://moodyinformatics.000webhostapp.com/product_full_info.php?id=" + idProduit);
@Override
protected Void doInBackground(Void... params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(productUrl).build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
prod = new Product(object.getInt("id_produit"), object.getString("nom"), object.getString("description"), object.getInt("qte_stock"), object.getInt("prix"), object.getString("img_avant"), object.getString("img_arriere"), object.getString("img_cote"));
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
getSupportActionBar().setTitle(prod.getName());
ImageView imgAvant = (ImageView) findViewById(R.id.img_avant);
Glide.with(ConsultProductActivity.this).load(prod.getImg_avant()).into(imgAvant);
ImageView imgArriere = (ImageView) findViewById(R.id.img_arriere);
Glide.with(ConsultProductActivity.this).load(prod.getImg_arriere()).into(imgArriere);
ImageView imgCote = (ImageView) findViewById(R.id.img_cote);
Glide.with(ConsultProductActivity.this).load(prod.getImg_coté()).into(imgCote);
TextView prix = (TextView) findViewById(R.id.prix_consult_p);
prix.setText(prod.getPrice());
AppCompatTextView description = (AppCompatTextView) findViewById(R.id.description);
description.setText(prod.getDescription());
}
};
task.execute();
}
公共类ConsultProductActivity扩展了AppCompatActivity{
线状idProduit;
产品生产;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent=getIntent();
Bundle extra=intent.getExtras();
idProduit=extra.getString(“idProduit”);
Toast.makeText(ConsultProductActivity.this、idProduit、Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity\u consult\u product);
工具栏=(工具栏)findViewById(R.id.Toolbar\u rest);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
preparefulinfo();
}
公开作废预处理Linfo(){
AsyncTask任务=新建AsyncTask(){
String productUrl=新字符串(“http://moodyinformatics.000webhostapp.com/product_full_info.php?id=“+idProduit);
@凌驾
受保护的Void doInBackground(Void…参数){
OkHttpClient=新的OkHttpClient();
Request Request=newrequest.Builder().url(productUrl.build();
试一试{
Response=client.newCall(request.execute();
JSONArray数组=新的JSONArray(response.body().string());
对于(int i=0;i
你收到idProduit了吗是的,那么你得到了哪个id?我已经测试了那部分代码,它是正确的,我用祝酒词测试了它,问题是返回的产品总是空的!!!非常感谢!你是最好的:D
[
{
"id_produit":"12",
"nom":"Switch Ethernet TP-Link TL-SG105",
"description":"10/100/1000 : 5 ports Gigabit Contrôle de flux 802.3x : 802.3x Gestion QoS (Quality of Service) 802.1P : 802.1p",
"prix":"3900",
"qte_stock":"5",
"img_avant":"http://moodyinformatics.000webhostapp.com/images/12.2.jpg",
"img_arriere":"http://moodyinformatics.000webhostapp.com/images/12.3.jpg",
"img_cote":"http://moodyinformatics.000webhostapp.com/images/12.4.jpg"
}
]
public class ConsultProductActivity extends AppCompatActivity {
String idProduit;
Product prod;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extra = intent.getExtras();
idProduit = extra.getString("idProduit");
Toast.makeText(ConsultProductActivity.this, idProduit, Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_consult_product);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_rest);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
prepareFullInfo();
}
public void prepareFullInfo() {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
String productUrl = new String("http://moodyinformatics.000webhostapp.com/product_full_info.php?id=" + idProduit);
@Override
protected Void doInBackground(Void... params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(productUrl).build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
prod = new Product(object.getInt("id_produit"), object.getString("nom"), object.getString("description"), object.getInt("qte_stock"), object.getInt("prix"), object.getString("img_avant"), object.getString("img_arriere"), object.getString("img_cote"));
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
getSupportActionBar().setTitle(prod.getName());
ImageView imgAvant = (ImageView) findViewById(R.id.img_avant);
Glide.with(ConsultProductActivity.this).load(prod.getImg_avant()).into(imgAvant);
ImageView imgArriere = (ImageView) findViewById(R.id.img_arriere);
Glide.with(ConsultProductActivity.this).load(prod.getImg_arriere()).into(imgArriere);
ImageView imgCote = (ImageView) findViewById(R.id.img_cote);
Glide.with(ConsultProductActivity.this).load(prod.getImg_coté()).into(imgCote);
TextView prix = (TextView) findViewById(R.id.prix_consult_p);
prix.setText(prod.getPrice());
AppCompatTextView description = (AppCompatTextView) findViewById(R.id.description);
description.setText(prod.getDescription());
}
};
task.execute();
}