2016-12-31 9 views
0

はなど、そのJSONオブジェクトと値が<img>ようなタグからの組み合わせである必要があり<src><p> はその値を取り、Html.from()方法とテキストビューに入れたいです。Html.fromとJSONオブジェクトが機能していない取得

public class MainActivity extends AppCompatActivity { 

    ProgressDialog pDialog; 
    public String html; 
    public String sag; 

    private final String url = "http://memaraneha.ir/%db%8c%da%a9%d9%be%d8%a7%d8%b1%da%86%da%af%db%8c-%d9%87%d9%85%d8%a7%d9%87%d9%86%da%af%db%8c-%d8%b7%d8%b1%d8%a7%d8%ad%db%8c-%d8%af%d8%a7%d8%ae%d9%84%db%8c"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



     TextView htmlTextView = (TextView)findViewById(R.id.html_text); 


     htmlTextView.setText(Html.fromHtml(sag, null, null)); 

     new GetContacts().execute(); 

    } 
    public class GetContacts extends AsyncTask<Void, Void, Void> { 

     private String TAG = "erfan"; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Showing progress dialog 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Please wait..."); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 

      HttpHandler sh = new HttpHandler(); 


      // Making a request to url and getting response 
      String jsonStr = sh.makeServiceCall(url); 


      Log.e(TAG, "Response from url: " + jsonStr); 

      if (jsonStr != null) { 
       try { 

        JSONObject jsonObj = new JSONObject(jsonStr); 


        JSONObject c = jsonObj.getJSONObject("posts"); 


        html = String.valueOf(c.getJSONObject("content")); 


       } catch (final JSONException e) { 
        Log.e(TAG, "Json parsing error: " + e.getMessage()); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(getApplicationContext(), 
            "Json parsing error: " + e.getMessage(), 
            Toast.LENGTH_LONG) 
            .show(); 
         } 
        }); 
       } 
      } else { 
       Log.e(TAG, "Couldn't get json from server."); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Couldn't get json from server. Check LogCat for possible errors!", 
           Toast.LENGTH_LONG) 
           .show(); 
        } 
       }); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      pDialog.dismiss(); 
      sag=html; 
     } 

    } 
} 

をこの行からヌル例外を取得:

これは、これまでにしようと私のいずれかが

答えて

0

最終的に私の問題を解決してバックラインでこの行を変更しますhtml = String.valueOf(c.getJSONObject("content"));からhtml= c.getString("content");

1

を助けてくださいすることができます場合は、理由「のNULLポインタを持っ

htmlTextView.setText(Html.fromHtml(sag, null, null)); 

サグ"値をテキストビューに設定 あなたは単にtextViewをグローバルにすることができます

public TextView htmlTextView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



    TextView htmlTextView = (TextView)findViewById(R.id.html_text); 


    new GetContacts().execute(); 

} 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     pDialog.dismiss(); 
     htmlTextView.setText(Html.from(html,arg,arg)); 
    } 
関連する問題