PHP的Andr​​oid的微调 - JSONObject的不能转换为JSONArray [英] php android spinner - JSONObject cannot be converted to JSONArray

查看:178
本文介绍了PHP的Andr​​oid的微调 - JSONObject的不能转换为JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java编程初学者,我试图在PHP的结果加载到微调和我一直有JSONObject的这个问题不能转换为JSONArray。我已经在搜索网站上的尝试和转换这一点,但我似乎无法修复它。

I am a beginner at java programming, I am trying to load the php result into a spinner and I have been having this problem of JSONObject cannot be converted to JSONArray. I have search on the website to try and convert this but i cannot seem to fix it.

PHP的结果是:

{"ref_product_types":[{"product_type_code":"1","product_type_description":"Mobile Phone"},{"product_type_code":"2","product_type_description":"Toys"}]}

我已经在清单中添加此

i have added this in the manifest

<uses-permission android:name="android.permission.INTERNET"/>

下面是java code:

here is the java code:

package com.example.spinnert1;

public class Main extends Activity {

InputStream is=null;
String result=null;
String line=null;

String[] product_type_code, product_type_description;  

Spinner spinner1,spinner2;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
final List<String> list1 = new ArrayList<String>();
final List<String> list2 = new ArrayList<String>();

Button b=(Button) findViewById(R.id.button1);

b.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
{
                // TODO Auto-generated method stub

try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/epos_connect/get_all_product_type.php");
HttpResponse response = httpclient.execute(httppost);
Log.e("Fail 1", "3");

HttpEntity entity = response.getEntity();
Log.e("Fail 1", "4");

is = entity.getContent();
Log.e("Pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
finish();
}    


try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}    


try
{
JSONArray JA=new JSONArray(result);


JSONObject json= null;
product_type_code = new String[JA.length()];       
product_type_description = new String[JA.length()];

for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
product_type_code[i] = json.getString("product_type_code");
product_type_description[i]=json.getString("product_type_description");
}
Toast.makeText(getApplicationContext(), "sss",Toast.LENGTH_LONG).show();

for(int i=0;i<product_type_code.length;i++)
{
list1.add(product_type_code[i]);
list2.add(product_type_description[i]);
}

spinner_fn();

}
catch(Exception e)
{

Log.e("Fail 3", e.toString());
//login.this.finish();

}
}
});

}
private void spinner_fn() {
    // TODO Auto-generated method stub

    ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
                                    android.R.layout.simple_spinner_item, product_type_code);
    dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter1);


    ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(),
                                    android.R.layout.simple_spinner_item, product_type_description);
    dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(dataAdapter2);


    spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
    {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id)
    {
    // TODO Auto-generated method stub

    spinner2.setSelection(position);

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0)
    {
    // TODO Auto-generated method stub
    }

    });


    spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
    // TODO Auto-generated method stub

    spinner1.setSelection(position);

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    }
    });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    }

布局XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/country_arrays"
    android:prompt="@string/country_prompt" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btnSubmit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit" />

</LinearLayout>

感谢

推荐答案

它看起来像正在被返回的数据确实是一个JSON对象,而不是一个JSON数组; JSON数组嵌套在外部对象中,在ref_product_types键。

It looks like the data that is being returned is indeed a JSON object, rather than a JSON array; the JSON array is nested within the outer objects, in the "ref_product_types" key.

您需要使用类似:

JSONObject jsonObj = new JSONObject(result);
JSONArray ja = jsonObj.getJSONArray("ref_product_types")

这篇关于PHP的Andr​​oid的微调 - JSONObject的不能转换为JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆