先生,我在Android程序设计中非常非常棒。最近我试图从yts.arg创建api链接并尝试解析我的应用程序中的数据,以便我可以在我的应用程序中显示电影列表。我尝试了一切,但我总是得到这个异常org.json.JSONException:没有找到值.. 在下面,我正在添加一切,我需要帮助....我是新的堆栈溢出我尽我所能问一个gd问题.. 通过此活动,我正在调用我的片段:
package com.example.mehadi.pravashsirmat;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;
public class econd extends AppCompatActivity implements MaterialTabListener {
Toolbar toolbar;
MaterialTabHost materialTabHost;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_econd);
toolbar = (Toolbar) findViewById(R.id.ecotoolbar);
setSupportActionBar(toolbar);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
materialTabHost = (MaterialTabHost) findViewById(R.id.materialTabHost);
viewPager = (ViewPager) findViewById(R.id.viewpagerfreco);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
materialTabHost.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < adapter.getCount(); i++) {
materialTabHost.addTab(materialTabHost.newTab().setIcon(adapter.geticon(i)).setTabListener(this));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// NavUtils.navigateUpFromSameTask(this);
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(MaterialTab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(MaterialTab tab) {
}
@Override
public void onTabUnselected(MaterialTab tab) {
}
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public int[] pcons = {R.drawable.ic_account_circle_black_24dp1_user, R.drawable.ic_add_alert_black_24dp_alert, R.drawable.ic_home_black_24dp_home};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if (position == 0) {
fragment = new Fragment_a();
}
if (position == 1) {
fragment = new Fragment_b();
}
if (position == 2) {
fragment = new Fragment_c();
}
// Myfragment myfragment=new Myfragment();
// Myfragment myfragment=Myfragment.getInstance(position);
//myfragment.getInstance(position);
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
return getResources().getStringArray(R.array.tabs)[position];
}
public Drawable geticon(int position) {
return getResources().getDrawable(pcons[position]);
}
}
}
从这个片段我打电话给我的api:
package com.example.mehadi.pravashsirmat;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by MEHADI on 8/5/2016.
*/
public class Fragment_a extends Fragment {
TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.for_frag_a, container, false);
textView = (TextView) layout.findViewById(R.id.textView_fr_a);
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, "https://yts.ag/api/v2/list_movies.json",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Toast.makeText(getContext(), response.toString(), Toast.LENGTH_LONG).show();
//textView.setText(response.toString());
parseJasonresponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
return layout;
}
public void parseJasonresponse(JSONObject response) {
if (response == null || response.length() == 0) {
return;
}
try {
StringBuffer data = new StringBuffer();
JSONObject arrayob = response.getJSONObject("data:");
JSONArray arrayobfrmovies = arrayob.getJSONArray("movies:");
for (int i = 0; i < arrayobfrmovies.length(); i++) {
JSONObject currentmovies =arrayobfrmovies.getJSONObject(i);
String Title = currentmovies.getString("title:");
data.append(Title + "\n");
}
Toast.makeText(getContext(), data.toString(), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
//Toast.makeText(getContext(), , Toast.LENGTH_LONG).show();
textView.setText("Error=" + e);
e.printStackTrace();
}
}
}
我还在为电影列表添加jason代码:
答案 0 :(得分:0)
似乎JSON链接中的“数据”和“电影”和“标题”已发布。所以更改
“data:”with“data”和
“电影:”与“电影”和
“title:”with“title” 即改变
JSONObject arrayob = response.getJSONObject("data:");
JSONArray arrayobfrmovies = arrayob.getJSONArray("movies:");
for (int i = 0; i < arrayobfrmovies.length(); i++) {
JSONObject currentmovies =arrayobfrmovies.getJSONObject(i);
String Title = currentmovies.getString("title:");
data.append(Title + "\n");
}
带
JSONObject arrayob = response.getJSONObject("data");
JSONArray arrayobfrmovies = arrayob.getJSONArray("movies");
for (int i = 0; i < arrayobfrmovies.length(); i++) {
JSONObject currentmovies =arrayobfrmovies.getJSONObject(i);
String Title = currentmovies.getString("title");
data.append(Title + "\n");
}
答案 1 :(得分:0)
替换此
JSONObject arrayob = response.getJSONObject("data:");
JSONArray arrayobfrmovies = arrayob.getJSONArray("movies:");
带
JSONObject arrayob = response.getJSONObject("data");
JSONArray arrayobfrmovies = arrayob.getJSONArray("movies");
从电影和数据中删除分号。您必须提供excat json对象名称