将JSON响应解析为TextView不会显示在TextView

时间:2017-12-12 08:08:57

标签: java android json textview

我正在开发一个移动应用来从Thingspeak检索数据,我必须以下列格式获得JSON Object响应:

 {
  "created_at": "2014-02-26T21:27:21Z",
  "entry_id": 879455,
  "field1": "176",
  "field2": "28.195329087048833"
 }

我在logcat中收到了JSON响应,但我尝试检索的值没有出现在我的应用程序的TextView上。我在其他设备中尝试过这些代码(Lollipop就像我之前尝试过的那样是一个前Lollipop设备),但TextView中仍然没有出现任何值。

MainActivity.java

 package com.example.teerna.testproject;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.os.AsyncTask;
 import android.util.Log;
 import android.widget.TextView;
 import org.json.JSONException;
 import org.json.JSONObject;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.MalformedURLException;
 import java.io.BufferedReader;
 import java.io.IOException;

public class MainActivity extends AppCompatActivity {
  TextView tv;

   @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv =(TextView)findViewById(R.id.aTextView);
    new GetMethodDemo().execute("https://api.thingspeak.com/channels/357670/feeds/last");

}

public class GetMethodDemo extends AsyncTask<String , Void ,String> {
    String server_response;



    @Override
    protected String doInBackground(String... strings) {

        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();

            int responseCode = urlConnection.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                server_response = readStream(urlConnection.getInputStream());
                Log.v("CatalogClient", server_response);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    // Converting InputStream to String
    private String readStream(InputStream in) {
        BufferedReader reader = null;
        StringBuffer response = new StringBuffer();
        try {
            reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line = "";
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
        }   catch (IOException e) {
            e.printStackTrace();
        }   finally {
            if (reader != null) {
                try {
                    reader.close();
                }   catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return response.toString();
    }

    @Override
    protected void onPostExecute(String response) {
        super.onPostExecute(response);

        if (response != null) {
            //String item;
            try {
                JSONObject json = new JSONObject(response);
                String item = json.optString("created_at");
                tv.setText(item);

            } catch (JSONException j) {
                j.printStackTrace();
            }

         }

      }
   }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.teerna.testproject.MainActivity">

<TextView
    android:id="@+id/aTextView"
    android:layout_width="132dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

来自Jellybean设备的Logcat:

    12-12 12:04:17.762 15947-15947/? D/dalvikvm: Late-enabling CheckJNI
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.view.Window$Callback.onPointerCaptureChanged, referenced from method android.support.v7.view.WindowCallbackWrapper.onPointerCaptureChanged
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve interface method 15988: Landroid/view/Window$Callback;.onPointerCaptureChanged (Z)V
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve interface method 15990: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve interface method 15992: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve interface method 15996: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 704: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
    12-12 12:04:19.164 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
    12-12 12:04:19.174 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
    12-12 12:04:19.174 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 726: Landroid/content/res/TypedArray;.getType (I)I
    12-12 12:04:19.174 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
    12-12 12:04:19.214 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.createDeviceProtectedStorageContext, referenced from method android.support.v4.content.ContextCompat.createDeviceProtectedStorageContext
    12-12 12:04:19.214 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 485: Landroid/content/Context;.createDeviceProtectedStorageContext ()Landroid/content/Context;
    12-12 12:04:19.214 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.214 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.getCodeCacheDir, referenced from method android.support.v4.content.ContextCompat.getCodeCacheDir
    12-12 12:04:19.214 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 491: Landroid/content/Context;.getCodeCacheDir ()Ljava/io/File;
    12-12 12:04:19.214 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.getColor, referenced from method android.support.v4.content.ContextCompat.getColor
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 492: Landroid/content/Context;.getColor (I)I
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v4.content.ContextCompat.getColorStateList
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 493: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.getDataDir, referenced from method android.support.v4.content.ContextCompat.getDataDir
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 495: Landroid/content/Context;.getDataDir ()Ljava/io/File;
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.getDrawable, referenced from method android.support.v4.content.ContextCompat.getDrawable
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 496: Landroid/content/Context;.getDrawable (I)Landroid/graphics/drawable/Drawable;
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.getExternalCacheDirs, referenced from method android.support.v4.content.ContextCompat.getExternalCacheDirs
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 498: Landroid/content/Context;.getExternalCacheDirs ()[Ljava/io/File;
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.getExternalFilesDirs, referenced from method android.support.v4.content.ContextCompat.getExternalFilesDirs
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 500: Landroid/content/Context;.getExternalFilesDirs (Ljava/lang/String;)[Ljava/io/File;
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.getNoBackupFilesDir, referenced from method android.support.v4.content.ContextCompat.getNoBackupFilesDir
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 503: Landroid/content/Context;.getNoBackupFilesDir ()Ljava/io/File;
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.getObbDirs, referenced from method android.support.v4.content.ContextCompat.getObbDirs
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 505: Landroid/content/Context;.getObbDirs ()[Ljava/io/File;
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.isDeviceProtectedStorage, referenced from method android.support.v4.content.ContextCompat.isDeviceProtectedStorage
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 516: Landroid/content/Context;.isDeviceProtectedStorage ()Z
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.startForegroundService, referenced from method android.support.v4.content.ContextCompat.startForegroundService
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 531: Landroid/content/Context;.startForegroundService (Landroid/content/Intent;)Landroid/content/ComponentName;
    12-12 12:04:19.224 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.334 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild
    12-12 12:04:19.334 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16457: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
    12-12 12:04:19.334 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
    12-12 12:04:19.344 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList
    12-12 12:04:19.344 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 493: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
    12-12 12:04:19.344 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
    12-12 12:04:19.354 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/graphics/drawable/Icon;)
    12-12 12:04:19.354 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.ImageButton.setImageIcon, referenced from method android.support.v7.widget.AppCompatImageButton.setImageIcon
    12-12 12:04:19.354 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16480: Landroid/widget/ImageButton;.setImageIcon (Landroid/graphics/drawable/Icon;)V
    12-12 12:04:19.354 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0000
    12-12 12:04:19.354 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
    12-12 12:04:19.354 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 667: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
    12-12 12:04:19.354 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
    12-12 12:04:19.354 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
    12-12 12:04:19.354 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 669: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
    12-12 12:04:19.354 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
    12-12 12:04:19.464 15947-15947/com.example.teerna.testproject E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
    12-12 12:04:19.464 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve instanceof 193 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
    12-12 12:04:19.464 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.TextView.getAutoSizeMaxTextSize, referenced from method android.support.v7.widget.AppCompatTextView.getAutoSizeMaxTextSize
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16776: Landroid/widget/TextView;.getAutoSizeMaxTextSize ()I
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0006
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.TextView.getAutoSizeMinTextSize, referenced from method android.support.v7.widget.AppCompatTextView.getAutoSizeMinTextSize
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16777: Landroid/widget/TextView;.getAutoSizeMinTextSize ()I
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0006
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.TextView.getAutoSizeStepGranularity, referenced from method android.support.v7.widget.AppCompatTextView.getAutoSizeStepGranularity
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16778: Landroid/widget/TextView;.getAutoSizeStepGranularity ()I
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0006
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.TextView.getAutoSizeTextAvailableSizes, referenced from method android.support.v7.widget.AppCompatTextView.getAutoSizeTextAvailableSizes
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16779: Landroid/widget/TextView;.getAutoSizeTextAvailableSizes ()[I
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0006
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.TextView.getAutoSizeTextType, referenced from method android.support.v7.widget.AppCompatTextView.getAutoSizeTextType
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16780: Landroid/widget/TextView;.getAutoSizeTextType ()I
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0008
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.TextView.setAutoSizeTextTypeUniformWithConfiguration, referenced from method android.support.v7.widget.AppCompatTextView.setAutoSizeTextTypeUniformWithConfiguration
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16823: Landroid/widget/TextView;.setAutoSizeTextTypeUniformWithConfiguration (IIII)V
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0006
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.TextView.setAutoSizeTextTypeUniformWithPresetSizes, referenced from method android.support.v7.widget.AppCompatTextView.setAutoSizeTextTypeUniformWithPresetSizes
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16824: Landroid/widget/TextView;.setAutoSizeTextTypeUniformWithPresetSizes ([II)V
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0006
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.TextView.setAutoSizeTextTypeWithDefaults, referenced from method android.support.v7.widget.AppCompatTextView.setAutoSizeTextTypeWithDefaults
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16825: Landroid/widget/TextView;.setAutoSizeTextTypeWithDefaults (I)V
    12-12 12:04:19.484 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6f at 0x0006
    12-12 12:04:19.534 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.TextView.getAutoSizeStepGranularity, referenced from method android.support.v7.widget.AppCompatTextHelper.loadFromAttributes
    12-12 12:04:19.534 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16778: Landroid/widget/TextView;.getAutoSizeStepGranularity ()I
    12-12 12:04:19.534 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0197
    12-12 12:04:19.534 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.text.StaticLayout$Builder.obtain, referenced from method android.support.v7.widget.AppCompatTextViewAutoSizeHelper.createStaticLayoutForMeasuring
    12-12 12:04:19.534 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve static method 15266: Landroid/text/StaticLayout$Builder;.obtain (Ljava/lang/CharSequence;IILandroid/text/TextPaint;I)Landroid/text/StaticLayout$Builder;
    12-12 12:04:19.534 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x71 at 0x0014
    12-12 12:04:19.534 15947-15947/com.example.teerna.testproject I/dalvikvm: Could not find method android.widget.TextView.isInLayout, referenced from method android.support.v7.widget.AppCompatTextViewAutoSizeHelper.setRawTextSize
    12-12 12:04:19.534 15947-15947/com.example.teerna.testproject W/dalvikvm: VFY: unable to resolve virtual method 16815: Landroid/widget/TextView;.isInLayout ()Z
    12-12 12:04:19.534 15947-15947/com.example.teerna.testproject D/dalvikvm: VFY: replacing opcode 0x6e at 0x0021
    12-12 12:04:19.754 15947-15947/com.example.teerna.testproject D/libEGL: loaded /system/lib/egl/libGLES_java.so

                                                                            [ 12-12 12:04:19.764 15947:15947 D/         ]
                                                                            mem_init ++


                                                                            [ 12-12 12:04:19.764 15947:15947 D/         ]
                                                                            gHwMemAllocator client 3

                                                                            [ 12-12 12:04:19.764 15947:15947 D/         ]
                                                                            **** Using ION allocator ****


                                                                            [ 12-12 12:04:19.764 15947:15947 D/         ]
                                                                            registered SIGUSR1[10] for pid[15947]

                                                                            [ 12-12 12:04:19.764 15947:15947 D/         ]
                                                                            HwMemAllocatorImpl Static Counters 0 0


                                                                            [ 12-12 12:04:19.764 15947:15947 D/         ]
                                                                            HwMemAllocatorImpl[5184cdcc] totalDeviceAllocSize[0] totalFree[0] maxFree[0] in numSlabs[0]


                                                                            [ 12-12 12:04:19.774 15947:15947 D/         ]
                                                                            mem_init 5184cdcc--
    12-12 12:04:19.774 15947-15947/com.example.teerna.testproject D/ION: config: version(0x10000) secure(0xf000) 256M(0x22d) fast(0x608) hwwr(0x608)
    12-12 12:04:19.784 15947-15947/com.example.teerna.testproject D/MM_DEVICE: Waiting for mm thread to come up
    12-12 12:04:19.784 15947-15965/com.example.teerna.testproject D/MM_DEVICE: mm_device_thread starting
    12-12 12:04:19.814 15947-15947/com.example.teerna.testproject D/OpenGLRenderer: Enabling debug mode 0
    12-12 12:04:23.067 15947-15964/com.example.teerna.testproject V/CatalogClient: {"created_at":"2017-12-02T09:37:26Z","entry_id":39,"field1":"6519"}

来自Lollipop设备的Logcat:

enter image description here

两个设备在运行应用程序时显示相同的屏幕(TextView中未检索到任何值)。

1 个答案:

答案 0 :(得分:0)

您正在返回null,从server_response返回doInBackGround(String... strings)

public class GetMethodDemo extends AsyncTask<String , Void ,String> {
    String server_response;

    @Override
    protected String doInBackground(String... strings) {

        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();

            int responseCode = urlConnection.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                server_response = readStream(urlConnection.getInputStream());
                Log.v("CatalogClient", server_response);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return server_response; //this should return server_response
    }

    // Converting InputStream to String
    private String readStream(InputStream in) {
        BufferedReader reader = null;
        StringBuffer response = new StringBuffer();
        try {
            reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line = "";
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
        }   catch (IOException e) {
            e.printStackTrace();
        }   finally {
            if (reader != null) {
                try {
                    reader.close();
                }   catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return response.toString();
    }

    @Override
    protected void onPostExecute(String response) {
        super.onPostExecute(response);

        if (response != null) {
            //String item;
            try {
                JSONObject json = new JSONObject(response);
                String item = json.optString("created_at");
                tv.setText(item);

            } catch (JSONException j) {
                j.printStackTrace();
            }

         }

      }
   }