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();
}
}
}
这是我的代码,我从服务器数据库I文件类型变量存储获取的文件路径,然后处理它。但是我无法得到我预期的输出,即文件本身......代码抛出异常我得到了
没有android.content.ActivityNotFoundException:找不到任何活动 处理Intent {act = android.intent.action.VIEW dat = file:/// http:/127.0.0.1/45_Hindi Theme 1&amp; 2(第IX类).pdf typ = application / pdf}
请帮我解决这个问题。
答案 0 :(得分:1)
此错误很简单,您的设备没有任何响应意图的应用程序。
您应该处理异常并告诉用户他在设备上有任何PDF阅读器。
答案 1 :(得分:0)
你需要将该代码移动到AsyncTask类中,因为你不能在主线程上调用互联网连接。
所以将所有这些代码移到某个函数中,比如getPdf();
并粘贴此
new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
getPdf();
return null;
}
}.execute();
并且您还需要AndroidManifest.xml文件中的互联网权限
<uses-permission android:name="android.permission.INTERNET" />