答案 0 :(得分:2)
答案 1 :(得分:0)
*package com.example;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class GetFromURLActivity extends Activity
{
TextView txtContent;
String path;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
//this is the file you want to download from the remote server
/*path ="http://localhost:82/TextureTest01.apk";*/
path ="http://10.0.2.2:82/my.txt";
//this is the name of the local file you will create
/*String targetFileName = "mynew"; // Omit extension.
boolean eof = false;*/
URL u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
InputStream in = c.getInputStream();
Log.e("value",in.toString());
//Gets the instance of the asset manager
AssetManager mngr=getAssets();
//Resources resources = new Resources();
//resources.getAssets().open("my.txt");
//FileOutputStream f = new FileOutputStream(new File("c:\\" + targetFileName));
/*FileOutputStream f = this.openFileOutput(targetFileName,
this.MODE_WORLD_READABLE);*/
ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
/*int len1 = 0;
while ( (len1 = in.read(buffer)) != -1 )
{
//f.write(buffer,0, len1);
bo.write(buffer,0, len1); // Write Into Buffer.
}*/
txtContent =(TextView)findViewById(R.id.textview);
txtContent.setText(bo.toString());
bo.close();
//f.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
//txtContent.setText(e.getMessage().toString());
} catch (ProtocolException e) {
e.printStackTrace();
//txtContent.setText(e.getMessage().toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
//txtContent.setText(e.getMessage().toString());
} catch (IOException e) {
e.printStackTrace();
//txtContent.setText(e.getMessage().toString());
}
}
String ReadFile(InputStream is)
{
ByteArrayOutputStream bo=new ByteArrayOutputStream();
byte [] buffer=new byte[1024];
try {
is.read(buffer);
bo.write(buffer);
bo.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bo.toString();
}
}
// My Email Id is muhammad.mubashir.bscs@gmail.com*