错误FATAL EXCEPTION主,http请求

时间:2013-12-30 06:50:27

标签: android database http exception

请帮我,错误致命的主要

新活动开始时我遇到此错误。

我的代码:

package com.novator.inweld;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;

public class News extends Activity
{
    @SuppressLint({ "NewApi", "ResourceAsColor" }) @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news);

        StrictMode.enableDefaults();

        String result = "";
        InputStream isr = null;

        try
        {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://test.ivanov-vladimir.com/get_news.php");
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            isr = entity.getContent();
        }
        catch(Exception e)
        {
            Log.e("log_tag", "HTTP connect error " + e.toString());
        }

        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "utf-8"), 8);
            StringBuilder builder = new StringBuilder();
            String line = null;

            while((line = reader.readLine()) != null)
            {
                builder.append(line + "\n");
                Log.v("line", line);
            }

            isr.close();
            result = builder.toString();
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Data converting error " + e.toString());
        }

        try
        {
            JSONArray jarray = new JSONArray(result);
            String s;
            TextView title;
            int textId;

            for(int i = 0; i < jarray.length(); i++)
            {
                textId = i + 1;

                JSONObject json = jarray.getJSONObject(i);
                s = json.getString("title") + "  -  " + json.getString("date") + "\n\n" + json.getString("content");

                title = new TextView(this);
                title.setTextColor(getResources().getColor(R.color.colorWihte));
                title.setShadowLayer(2, 0, 0,getResources().getColor(R.color.colorBlack));
                title.setBackground(getResources().getDrawable(R.drawable.round_block));
                title.setId(textId);
                title.setText(s);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

                if(i == 0)
                {

                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    title.setLayoutParams(layoutParams);
                }
                else
                {
                    layoutParams.addRule(RelativeLayout.BELOW, textId - 1);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    layoutParams.setMargins(0, 20, 0, 0);
                    title.setLayoutParams(layoutParams);
                }

                ((ViewGroup) findViewById(R.id.newslayout)).addView(title);
            }
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Data parsing error " + e.toString());
        }
    }
}

日志:

12-30 10:18:21.673: D/dalvikvm(324): VFY: replacing opcode 0x71 at 0x000d
12-30 10:18:21.683: D/dalvikvm(324): VFY: dead code 0x0010-01cd in Lcom/novator/inweld/News;.onCreate (Landroid/os/Bundle;)V
12-30 10:18:21.873: D/dalvikvm(324): GC_EXTERNAL_ALLOC freed 2959 objects / 152376 bytes in 148ms
12-30 10:18:22.583: D/AndroidRuntime(324): Shutting down VM
12-30 10:18:22.583: W/dalvikvm(324): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-30 10:18:22.603: E/AndroidRuntime(324): FATAL EXCEPTION: main
12-30 10:18:22.603: E/AndroidRuntime(324): java.lang.NoClassDefFoundError: android.os.StrictMode
12-30 10:18:22.603: E/AndroidRuntime(324):  at com.novator.inweld.News.onCreate(News.java:32)
12-30 10:18:22.603: E/AndroidRuntime(324):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-30 10:18:22.603: E/AndroidRuntime(324):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-30 10:18:22.603: E/AndroidRuntime(324):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-30 10:18:22.603: E/AndroidRuntime(324):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-30 10:18:22.603: E/AndroidRuntime(324):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-30 10:18:22.603: E/AndroidRuntime(324):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 10:18:22.603: E/AndroidRuntime(324):  at android.os.Looper.loop(Looper.java:123)
12-30 10:18:22.603: E/AndroidRuntime(324):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-30 10:18:22.603: E/AndroidRuntime(324):  at java.lang.reflect.Method.invokeNative(Native Method)
12-30 10:18:22.603: E/AndroidRuntime(324):  at java.lang.reflect.Method.invoke(Method.java:521)
12-30 10:18:22.603: E/AndroidRuntime(324):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-30 10:18:22.603: E/AndroidRuntime(324):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-30 10:18:22.603: E/AndroidRuntime(324):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您应该使用threadAsynctask进行与网络相关的操作

http://developer.android.com/reference/android/os/AsyncTask.html

阅读主题上述链接中的4个步骤。

http://developer.android.com/reference/android/os/StrictMode.html

StrictMode是一个开发人员工具,可以检测到您可能偶然发生的事情,并引起您的注意,以便您可以解决这些问题

调用Asynctask

  new TheTask().execute();

 class TheTask extends Asynctask<Void,String,String>
  {
      @Override
      public String doInbackground(Void... params)
      {
        String response=null;  
        try
        {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://test.ivanov-vladimir.com/get_news.php");
        HttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        response = EntityUtils.toString(entity); 
        }
        catch(Exception e)
        {
        Log.e("log_tag", "HTTP connect error " + e.toString());
        }
       return response;
      } 
      @Override 
      protected void onPostExecute(String result)
      {
          super.onPostExecute(result);
          Log.i("Result is ....",result); 
      }  
  } 

注意:您无法从后台线程更新ui。使用onPostExecute更新ui

除了在for循环中创建textview之外,您还可以使用listview。

您还可以使用自定义适配器使用textview填充布局并为textview设置样式。

答案 1 :(得分:0)

问题与您的课程路径有关。 NoClassDefFoundError

  

如果Java虚拟机或ClassLoader实例尝试加载类的定义(作为普通方法调用的一部分或作为使用新表达式创建新实例的一部分),则抛出该类,并且没有类的定义可以被发现。

     

在编译当前正在执行的类时存在搜索的类定义,但无法再找到该定义。

Fore More Details

如何解决这个问题?

检查ThisThisThis

除此之外,您正试图在您的UI线程中调用您的WebAPI,而该线程一点都不好。

尝试使用AsyncTask,以便在后台加载您的数据,而不会影响您的用户界面。Check Sample Example.