1

have json object and value of that is combination from tags like <img>, <src>, <p> etc. want take that value and put in text view with Html.from() way.

this my try so far:

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;
        }

    }
}

but get null exception from this line :

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

if any one can please help

2 Answers 2

1

you got null pointer because of "sag" value that set into text view you can simply make your textView global

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));
    }
Sign up to request clarification or add additional context in comments.

Comments

0

finally solve my problem with change this line in do in backgorund html = String.valueOf(c.getJSONObject("content")); to html= c.getString("content");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.