このコードを追加する前に、私のアプリは問題なく動作します。このコードを使用して別の Android プロジェクトを試してみましたが、問題なく動作します...
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/jan/android/fave.php");
try {
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
JSONObject object = new JSONObject(jsonResult);
JSONArray the_json_array = object.getJSONArray("products");
int length = the_json_array.length();
for (int i = 0; i < length; i++)
{
JSONObject another_json_object = the_json_array.getJSONObject(i);
String verion = another_json_object.optString("title");
}
}
catch (JSONException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
これは入力ストリーム用です
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
return answer;
}
しかし、このコードに追加すると、アプリがクラッシュします。これを 12 時間以上デバッグしていますが、修正にはほど遠い状態です。
public class MainActivity extends Activity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
setContentView(R.layout.activity_main);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/jan/android/fave.php");
try {
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
JSONObject object = new JSONObject(jsonResult);
JSONArray the_json_array = object.getJSONArray("products");
int length = the_json_array.length();
for (int i = 0; i < length; i++)
{
JSONObject another_json_object = the_json_array.getJSONObject(i);
String verion = another_json_object.optString("title");
}
}
catch (JSONException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
// Create an instance of editorFrag
mainFragment firstFrag = new mainFragment();
// add fragment to the fragment container layout
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction().add(R.id.content_frame, firstFrag);
ft.commit();
// 1. pass context and data to the custom adapter
MyAdapter adapter = new MyAdapter(this, generateData());
// if extending Activity 2. Get ListView from activity_main.xml
ListView listView = (ListView) findViewById(R.id.listview);
// 3. setListAdapter
//listView.setAdapter(adapter); if extending Activity
listView.setAdapter(adapter);
// App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
// Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
// just styling option add shadow the right edge of the drawer
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
listView.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
private ArrayList<Model> generateData(){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/janime/android/fave.php");
ArrayList<Model> models = new ArrayList<Model>();
models.add(new Model("SmileyChix", "http://fc05.deviantart.net/fs71/i/2013/001/3/8/ouma_shu___guilty_crown_wallpaper_lost_christmas_by_akije_hirodi-d5q45lh.jpg"));
models.add(new Model(R.drawable.ic_action_dock,"Latest",null));
models.add(new Model(R.drawable.ic_action_storage,"Watchlist","12"));
models.add(new Model(R.drawable.ic_action_settings,"Settings",null));
try
{
String the_json = "{'products':[{'title':'test'},{'title':'test'}],'success':1}";
JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("products");
int length = the_json_array.length();
for (int i = 0; i < length; i++)
{
JSONObject another_json_object = the_json_array.getJSONObject(i);
String artist_name = another_json_object.optString("title").toString();
models.add(new Model(R.drawable.ic_drawer, artist_name,"12"));
}
}
catch (JSONException e) {
e.printStackTrace();
}
return models;
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
TextView text = (TextView) view.findViewById(R.id.passId);
String tEXT = text.getText().toString();
Toast.makeText(MainActivity.this, tEXT, Toast.LENGTH_LONG).show();
// Create an instance of editorFrag
videoList firstFrag = new videoList();
Bundle bundle = new Bundle();
bundle.putString("key", tEXT);
firstFrag.setArguments(bundle);
// add fragment to the fragment container layout
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction().add(R.id.content_frame, firstFrag);
ft.addToBackStack(null);
ft.commit();
drawerLayout.closeDrawers();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
return answer;
}
}
ログキャット
11-07 21:50:49.987: E/AndroidRuntime(30662): FATAL EXCEPTION: main
11-07 21:50:49.987: E/AndroidRuntime(30662): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.janime/android.janime.MainActivity}: android.os.NetworkOnMainThreadException
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.app.ActivityThread.access$700(ActivityThread.java:159)
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.os.Looper.loop(Looper.java:137)
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.app.ActivityThread.main(ActivityThread.java:5419)
11-07 21:50:49.987: E/AndroidRuntime(30662): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 21:50:49.987: E/AndroidRuntime(30662): at java.lang.reflect.Method.invoke(Method.java:525)
11-07 21:50:49.987: E/AndroidRuntime(30662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
11-07 21:50:49.987: E/AndroidRuntime(30662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-07 21:50:49.987: E/AndroidRuntime(30662): at dalvik.system.NativeStart.main(Native Method)
11-07 21:50:49.987: E/AndroidRuntime(30662): Caused by: android.os.NetworkOnMainThreadException
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
11-07 21:50:49.987: E/AndroidRuntime(30662): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
11-07 21:50:49.987: E/AndroidRuntime(30662): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-07 21:50:49.987: E/AndroidRuntime(30662): at libcore.io.IoBridge.connect(IoBridge.java:112)
11-07 21:50:49.987: E/AndroidRuntime(30662): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-07 21:50:49.987: E/AndroidRuntime(30662): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-07 21:50:49.987: E/AndroidRuntime(30662): at java.net.Socket.connect(Socket.java:842)
11-07 21:50:49.987: E/AndroidRuntime(30662): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
11-07 21:50:49.987: E/AndroidRuntime(30662): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
11-07 21:50:49.987: E/AndroidRuntime(30662): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-07 21:50:49.987: E/AndroidRuntime(30662): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-07 21:50:49.987: E/AndroidRuntime(30662): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-07 21:50:49.987: E/AndroidRuntime(30662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
11-07 21:50:49.987: E/AndroidRuntime(30662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
11-07 21:50:49.987: E/AndroidRuntime(30662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.janime.MainActivity.onCreate(MainActivity.java:91)
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.app.Activity.performCreate(Activity.java:5372)
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
11-07 21:50:49.987: E/AndroidRuntime(30662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
11-07 21:50:49.987: E/AndroidRuntime(30662): ... 11 more