how to resolve host exception? - android

please take me out this problem ,I am trying to get file from Ftp server .
here is my code which i am continuously getting exception like
java.net.unknownHostException :ftpbrasnet.no-ip.org.
i have tried in browser its working.
FTPClient client = new FTPClient();
FileOutputStream Foutput = null;
try {
client.connect("ftp://ftpbrasnet.no-ip.org/"); // My Exceptionis here
client.login("win7", "123");
// Create an OutputStream for the file
String filename = "files.txt";
Foutput = new FileOutputStream(filename);
// Fetch file from server
client.retrieveFile("/" + filename, Foutput);
}
catch (IOException e)
{
Log.e("ERROR", e.getStackTrace().toString());
}
finally
{
try
{
if (Foutput != null) {
Foutput.close();
}
client.disconnect();
} catch (IOException e) {
Log.e("ERROR", e.getStackTrace().toString());
}
}
}
and here is my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Net.estoque"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="#string/app_name" >
<activity android:name="Netestoque"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

Have you tried removing the protocol from the URL, like so:
client.connect("ftpbrasnet.no-ip.org/");

Related

AsyncTask Permission denied (missing INTERNET permission?)

I am trying use AsyncTask to extract an image over the internet and display on the RecyclerViewer but I am getting the following error message, which's captured from my Exception in my doInBackground from my RecyclerViewAdapter class:
08-02 00:11:25.608: E/ImageDownload(5753): Download failed: Permission denied (missing INTERNET permission?)
See full version of AsyncTask sub-class below:
class GetImageFromNet extends AsyncTask<String, Void, Bitmap> {
private RVAdapter.PersonViewHolder personViewHolder;
private String url = "http://ia.media-imdb.com/images/M/MV5BNDMyODU3ODk3Ml5BMl5BanBnXkFtZTgwNDc1ODkwNjE#._V1_SX300.jpg";
public GetImageFromNet(RVAdapter.PersonViewHolder personViewHolder){
this.personViewHolder = personViewHolder;
}
protected Bitmap doInBackground(String... params) {
try {
InputStream in = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
return bitmap;
//NOTE: it is not thread-safe to set the ImageView from inside this method. It must be done in onPostExecute()
} catch (Exception e) {
Log.e("ImageDownload", "Download failed: " + e.getMessage());
}
return null;
}
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
personViewHolder.personPhoto.setImageBitmap(bitmap);
}
I have no idea why I am getting above error since I have already added the following permission access in my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Please help!
Move your <uses-permission> elements to be immediate children of <manifest>, above your <application> element.
Update your manifest as:-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

BlueTooth Connect to a embedded device that requires a PIN

I am writing and Android app that would detect and connect to a know embedded device. The issue my side is that it throws exception whenever I am trying to connect:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
try {
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(getApplicationContext(),"Try Connecting to device : " + device.getName(),
Toast.LENGTH_LONG).show();
byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "1234");
BluetoothDevice.class.getMethod("setPin", byte[].class).invoke(device, pin);
BlueToothConnect(device);
}catch(Exception e){}
}
}
};
And my BlueToothConnect method is :
public void BlueToothConnect(BluetoothDevice device){
if (deviceName.equals(device.getName()))
{
try
{
//mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mSocket = (BluetoothSocket) m.invoke(device, 1);
mSocket.connect();
GetConnectionDetails();
result = device;
blueTooth_On_off = 1;
}
catch(Exception e){
text.setText(getStackTrace(e));
try{
result = null;
mSocket.close();
blueTooth_On_off = 0;
}
catch(IOException e1){
result = null;
blueTooth_On_off = 0;
}
}
}
}
I am getting below exception error:
java.io.exception:[jsr82] connect: Connection is not created (failed or aborterd)
I cannot do much on embedded device side either.
Below is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.edazzelsmarthome"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.edazzelsmarthome.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".broadcast.PairingRequest">
<intent-filter>
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
</intent-filter>
</receiver>
</application>
</manifest>

Android raw resource file not readable

I have got several mp3 files added as raw resource to my project. I would like to play them with AudioPlayer. I had FileNotfoundException and I backtraced it to find that the files can be found but they are not readable:
mp1.stop();
mp1.reset();
playedSound = R.raw.sip02;
String str = getResources().getString(playedSound);
File file = new File(str);
//((File)file).setReadable(true);
boolean readable = file.canRead(); //returns TRUE
file.setReadOnly();
readable = file.canRead(); //returns TRUE
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace(); //enters this branch
}
I cannot call file.setReadable() because I use API level 7.
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.movingcircle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.movingcircle.MovingCircle"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Try
MediaPlayer mPlayer = MediaPlayer.create(PlayWorld.this, R.raw.your_mp3);

Translate in Android using Google API

I want to create a translate app in Android,i use Google Translate API,but it has error,please help me for fix it. This is my code
public void onClick(View v) {
// TODO Auto-generated method stub
String InputString;
String OutputString = null;
InputString = InputText.getText().toString();
try {
Translate.setHttpReferrer("http://translate.google.com.vn/");
OutputString = Translate.DEFAULT.execute(InputString, Language.ENGLISH, Language.VIETNAMESE); ////////
} catch (Exception ex) {
ex.printStackTrace();
OutputString = "Error";
}
OutputText.setText(OutputString);
}
Here is my Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.stthing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Error in line Translate.setHttpReferrer("http://translate.google.com.vn/");
The method setHttpReferrer(String) is undefined for the type Translate
I am not sure if you are aware but Translate API is no longer free. so you would need to set the API key as well..
instead of using
Translate.setHttpReferrer("http://translate.google.com.vn/");
use
GoogleAPI.setHttpReferrer("http://translate.google.com.vn/");
GoogleAPI.setKey(/* Enter your API key here */);

Creating BluetoothSocket crashes with "Could not execute method of the activity"

long uuidHigh = new BigInteger("11bc4f7154ac9edb", 16).longValue();
long uuidLow = new BigInteger("167bf05f9436bc0a", 16).longValue();
UUID uuid = new UUID(uuidHigh, uuidLow);
BluetoothSocket socket = null;
BluetoothAdapter blueAdapter = null;
blueAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = blueAdapter.getRemoteDevice("00:07:AB:6A:96:7D");
try {
socket = device.createRfcommSocketToServiceRecord(uuid); // CRASH!
} catch (IOException e) { e.printStackTrace(); }
Result:
E/AndroidRuntime(14079): java.lang.IllegalStateException: Could not execute method of the activity
Statements just before the "socket =" statement in the same method execute fine. Getting the device seems to work fine with getName() working on it.
The physical device it runs on has API 8.
The bluetooth dev guide cryptically says:
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
But createRfcommSocketToServiceRecord() cannot take a String, it must take a UUID.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="intrax.demo.explore"
android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" >
<activity
android:name=".BluetoothAct" android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Categories

Resources