我需要将json数据从localhost馈送到可搜索的微调器。数据传输正常,但适配器似乎抛出错误。
import com.toptoche.searchablespinnerlibrary.SearchableSpinner;
public class SearchActivity extends AppCompatActivity {
private static final String TAG = "SearchActivity";
TextView nameTextView;
TextView dosaTextView;
SearchableSpinner nameSpinner;
SearchableSpinner dosageSpinner;
String[] name = new String[5000];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
nameSpinner = findViewById(R.id.nameSpinner);
dosageSpinner = findViewById(R.id.dosageSpinner);
final String nameApiLink = "http://192.168.10.50:80/xxx/xxx.php";
class FetchJsonData extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL(nameApiLink);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
//ArrayList<String> sb = new ArrayList<>();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String jsonString;
while ((jsonString = bufferedReader.readLine()) != null) {
//sb.add(jsonString);
sb.append(jsonString);
}
JSONArray jsonArray = new JSONArray(sb.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonName = jsonArray.getJSONObject(i);
name[i] = jsonName.getString("PRODUCT_COMPOUND_NAME");
}
//return sb;
} catch (Exception e) {
e.getCause();
}
return null;
}
}
FetchJsonData fetchJsonDataForName = new FetchJsonData();
fetchJsonDataForName.execute();
ArrayAdapter spinnerAdapterForName = new ArrayAdapter<>(SearchActivity.this, android.R.layout.simple_spinner_dropdown_item, name);
nameSpinner.setAdapter(spinnerAdapterForName);
}
}
数据看起来像这样。
[{"PRODUCT_COMPOUND_CODE":"000001","PRODUCT_COMPOUND_NAME":"---"},{"PRODUCT_COMPOUND_CODE":"1","PRODUCT_COMPOUND_NAME":"LUTEINIZING HORMONE"},{"PRODUCT_COMPOUND_CODE":"100","PRODUCT_COMPOUND_NAME":"EMBLICA OFFICINALIS "},{"PRODUCT_COMPOUND_CODE":"1000","PRODUCT_COMPOUND_NAME":"ISBUFYLLINE "},{"PRODUCT_COMPOUND_CODE":"1002","PRODUCT_COMPOUND_NAME":"LEVEMOPAMIL "},{"PRODUCT_COMPOUND_CODE":"1003","PRODUCT_COMPOUND_NAME":"LEVETIRACETAM "}
运行代码时,出现以下错误。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kalashtech.DDA, PID: 30787
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:453)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:415)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:204)
at android.widget.Spinner.onMeasure(Spinner.java:602)
at android.view.View.measure(View.java:23196)
at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:23196)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:23196)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23196)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23196)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23196)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6753)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:720)
at android.view.View.measure(View.java:23196)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2814)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1662)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1946)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1550)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7437)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1089)
at android.view.Choreographer.doCallbacks(Choreographer.java:885)
at android.view.Choreographer.doFrame(Choreographer.java:816)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1075)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6821)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
请帮助。我是新来的人,所以如果有什么问题,那么抱歉。
答案 0 :(得分:2)
您将需要ArrayList
,并且必须像这样初始化和分配内存,其中AsyncTask
拥有其中的部分callback methods
。
final ArrayList<String> arrayList = new ArrayList<>();
final ArrayList<String> arrayList = new ArrayList<>();
class FetchJsonData extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(Void... voids) {
try {
URL url = new URL(nameApiLink);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
//ArrayList<String> sb = new ArrayList<>();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String jsonString;
while ((jsonString = bufferedReader.readLine()) != null) {
//sb.add(jsonString);
sb.append(jsonString);
}
JSONArray jsonArray = new JSONArray(sb.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonName = jsonArray.getJSONObject(i);
arrayList.add(jsonName.getString("PRODUCT_COMPOUND_NAME"));
}
return arrayList;
//return sb;
} catch (Exception e) {
e.getCause();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<String> stringList) {
super.onPostExecute(stringList);
if(stringList!=null && stringList.size()>0)
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, stringList );
nameSpinner.setAdapter(adapter);
nameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String product_name = (String) parent.getSelectedItem();
Toast.makeText(view.getContext(), "PRODUCT_NAME--"+product_name , Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
}
FetchJsonData fetchJsonDataForName = new FetchJsonData();
fetchJsonDataForName.execute();
您可以创建dataclass
,然后与this之类的ArrayAdapter一起使用