0
package com.nitrr.nrnotifier;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.utils.URLEncodedUtils;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

 import android.app.Activity;
 import android.content.Intent;
 import android.net.ParseException;
 import android.net.Uri;
 import android.os.Bundle;
 import android.util.Log;
 import android.webkit.MimeTypeMap;
 import android.widget.Toast;

public class Act3 extends Activity
{
 static InputStream is = null;
 String result = null;
  public void onCreate(Bundle savedInstanceState)
  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third);
        String id=getIntent().getExtras().getString("pid");
        try
        {
             List<NameValuePair> params = new ArrayList<NameValuePair>();
             params.add(new BasicNameValuePair("id", id));
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            String url="http://10.0.2.2/ndesc.php";
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }
        catch(Exception se){
            Log.e("log_tag", "Error in http connection"+se.toString());
        }

        //convert response to string
        try{

        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
              StringBuilder sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line="0";

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            result = sb.toString();
           System.out.println(result);

        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }




        try{
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data = null;

            for(int i = 0; i < jArray.length(); i++){
                    json_data = jArray.getJSONObject(i);
                   String filepath=json_data.getString("ndesc");
                   Uri fileUri = Uri.fromFile(new File(filepath));

                   Uri new_uri = Uri.parse("file://"
                           + fileUri.getPath());

                   Intent intent = new Intent(Intent.ACTION_VIEW,
                           new_uri);

                   MimeTypeMap myMime = MimeTypeMap.getSingleton();
                   String fileExt = filepath.substring(filepath
                           .lastIndexOf(".") + 1);
                   String mimeType = myMime
                           .getMimeTypeFromExtension(fileExt);
                   intent.setDataAndType(new_uri, mimeType);

                   startActivity(intent);



            }

            }catch(JSONException e2)
            {  e2.printStackTrace();
                Toast.makeText(getBaseContext(), "No User Found", Toast.LENGTH_LONG).show();
            }catch (ParseException e1){
                e1.printStackTrace();
            } 
 }


}

This is my code where I'm storing fetched filepath from server database I filetype variable and then processing it..But I couldn't get my expected output i.e. the file itself..the code is throwing Exception and I get

no android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///http:/127.0.0.1/45_Hindi Theme 1 & 2 (Class-IX).pdf typ=application/pdf }

please help me to fix this.

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
Puru
  • 1
  • 1
  • 4

2 Answers2

1

This errors is simple that your device dont have any application that respond to the intent.

You should handle the Exception and tell the user that he doenst have any PDF reader on the device.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

U need to move that code into the AsyncTask class, because u cannot invoke internet connection on main thread.

so move all this code to some function, like getPdf();

and paste this

        new AsyncTask<Void, Void, Void>(){


        @Override
        protected Void doInBackground(Void... params) {
            getPdf();
            return null;
        }
    }.execute();

and u also need internet permission in your AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET" />
Vaizadoo
  • 330
  • 2
  • 9
  • i already gave internet permission in my manifest file..and on writing the entire code in an asynchronous task doesn't change the output..i still receive json exception.. – Puru Jun 11 '15 at 16:22
  • C:\Users\USER\Downloads\45_Hindi Theme 1 & 2 (Class-IX).pdf – Puru Jun 12 '15 at 11:50
  • this is what my result string shows..which is the path of pdf file with respect to selected id i stored in my database..so my result is not null..hence something wrong is with code JSONArray jArray = new JSONArray(result) which is throwing exception.. – Puru Jun 12 '15 at 11:50
  • if result is "C:\Users\USER\Downloads\45_Hindi Theme 1 & 2 (Class-IX).pdf" than that is not correct json array structure. Thats why u get exception. U are downloading wrong content. – Vaizadoo Jun 12 '15 at 11:55
  • no sorry i made some mistake in my php file,,so after edits i get result=[{"ndesc","http:/127.0.0.1/45_Hindi Theme 1 & 2 (Class-IX).pdf "}].. – Puru Jun 12 '15 at 20:22
  • than u need to download this file: here is link how to do that: http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog and please if i helped u rate my answer :) – Vaizadoo Jun 12 '15 at 22:27