如果您对从PHP服务器获取图像和元数据到android感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于从PHP服务器获取图像和元数据到android的详细内容,我们还将为您解
如果您对从PHP服务器获取图像和元数据到android感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于从PHP服务器获取图像和元数据到android的详细内容,我们还将为您解答php获取服务器信息的相关问题,并且为您提供关于android – 如何从字节数组上传数据到PHP服务器上传进度?、android – 如何从服务器获取数据并使用它更新数据库?、android 上传图片到php服务器、android 客户端 与 php服务器通信的有价值信息。
本文目录一览:- 从PHP服务器获取图像和元数据到android(php获取服务器信息)
- android – 如何从字节数组上传数据到PHP服务器上传进度?
- android – 如何从服务器获取数据并使用它更新数据库?
- android 上传图片到php服务器
- android 客户端 与 php服务器通信
从PHP服务器获取图像和元数据到android(php获取服务器信息)
但是现在我还需要获取一个图像以及从数据库中获取的每一行信息.我得到的信息有一个字段,它指定对应图像在文件系统中的路径.
所以,一旦我得到图像的路径,我如何阅读它们,以便我可以将它们与获得的信息行一起发送?我可以将JSON与信息一起编码吗?或者,如果我在Android应用程序中有信息,我应该用readfile逐个阅读它们吗?如果可以使用JSON完成,那么如何解析图像的数据呢?你能提供一个例子吗?
解决方法
android – 如何从字节数组上传数据到PHP服务器上传进度?
http://loopj.com/android-async-http/
我想要的是在进度条上显示已上载字节数组的字节数.现在,问题是,如何在上传进行过程中了解已上传的字节数.需要有关想法/示例/代码问题的帮助.先谢谢你们.
解决方法
class ImageUploadTask extends AsyncTask<Void,Void,String> { @Override protected void onPreExecute() { pb.setVisibility(View.VISIBLE); } @Override protected String doInBackground(Void... unused) { String twoHyphens = "--"; String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****"; String lineEnd = "\r\n"; try { FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); URL url = new URL(urlServer); connection = (HttpURLConnection) url.openConnection(); // Allow Inputs & Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Enable POST method connection.setRequestMethod("POST"); connection.setRequestProperty("Connection","Keep-Alive"); connection.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary); outputStream = new DataOutputStream( connection.getoutputStream() ); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-disposition: form-data; name=\"image\";filename=\"" + pathToOurFile +"\"" + lineEnd); outputStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); Log.v("Size",bytesAvailable+""); pb.setProgress(0); pb.setMax(bytesAvailable); //Log.v("Max",pb.getMax()+""); bufferSize = Math.min(bytesAvailable,maxBufferSize); buffer = new byte[bufferSize]; // Read file bytesRead = fileInputStream.read(buffer,bufferSize); while (bytesRead > 0) { outputStream.write(buffer,bufferSize); bytesAvailable = fileInputStream.available(); Log.v("Available",bytesAvailable+""); publishProgress(); bufferSize = Math.min(bytesAvailable,maxBufferSize); bytesRead = fileInputStream.read(buffer,bufferSize); } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) serverResponseCode = connection.getResponseCode(); serverResponseMessage = connection.getResponseMessage(); System.out.println(serverResponseMessage); fileInputStream.close(); outputStream.flush(); outputStream.close(); } catch (Exception ex) { //Exception handling } //publishProgress(); return null; } @Override protected void onProgressUpdate(Void... unsued) { super.onProgressUpdate(unsued); pb.setProgress(pb.getMax()-bytesAvailable); } @Override protected void onPostExecute(String sResponse) { //if(pb.getProgress()>= pb.getMax()) pb.setVisibility(View.INVISIBLE); } }
android – 如何从服务器获取数据并使用它更新数据库?
我正在实现一个Android应用程序,我无法弄清楚如何解决这个问题:当用户点击更新按钮,我想连接到我的服务器,并检查数据是否有任何更新,如果有,我想从服务器获取数据并更新数据库.我应该使用xml结构检查并从服务器获取更新,还是有更聪明的方法来实现这一点?
解决方法:
是的,这是一种更明智的方法,但它需要在服务器端和客户端上进行一些工作.这是它的工作原理:所有数据都应该可以以JSON或XML格式下载(我更喜欢JSON).所以,你将在服务器中有这样的东西:http://example.com/resource.json.为了知道是否有新版本的数据,你可以做的是在HTTP响应中添加一个头版本(这样你就不用下载了并解析整个资源,以了解是否有新版本).
为了让您检查这些标题,您将拥有以下内容:
URLConnection urlConnection = null;
try {
urlConnection = url.openConnection();
urlConnection.connect();
String currentVersionHeader = urlConnection.getHeaderField("your-version-header");
if( currentVersionHeader == null ) {
currentVersionHeader = "-1";
}
int version = Long.parseLong(currentVersionHeader);
// then you compare with the old version
if( oldVersion < version ){
// download the data
}
} catch (Exception e) {}
下载和解析JSON资源已经得到了处理,你会在Google和这里找到一堆教程和参考资料.
您没有提供服务器端的详细信息(是PHP?Java ?. NET?),因此我也不会详细介绍如何实现/添加版本标头.我只是根据自己的经验向你解释了做这类事情的最好方法.
android 上传图片到php服务器
android代码
public class EX08_11 extends Activity
{
/* 变量声明
* newName:上传后在服务器上的文件名称
* uploadFile:要上传的文件路径
* actionUrl:服务器对应的程序路径 */
// private String newName="345444.jpg";
private String uploadFile="/sdcard/345444.jpg";
private String acti//*********/upload.php";
private TextView mText1;
private TextView mText2;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mText1 = (TextView) findViewById(R.id.myText2);
mText1.setText("文件路径:\n"+uploadFile);
mText2 = (TextView) findViewById(R.id.myText3);
mText2.setText("上传网址:\n"+actionUrl);
/* 设定mButton的onClick事件处理 */
mButton = (Button) findViewById(R.id.myButton);
mButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
uploadFile();
}
});
}
/* 上传文件吹Server的method */
private void uploadFile()
{
// String end = "\r\n";
// String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
// con.setReadTimeout(5 * 1000);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设定传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("enctype",
"multipart/form-data;boundary="+boundary);
/* 设定DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
/*ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=\"file1\";filename=\"" +
newName +"\"" + end);
ds.writeBytes(end); */
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设定每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
立即学习“PHP免费学习笔记(深入)”;
int length = -1;
/* 从文件读取数据到缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将数据写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
// ds.writeBytes(end);
// ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 将Response显示于Dialog */
showDialog(b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog(""+e);
}
}
/* 显示Dialog的method */
private void showDialog(String mess)
{
new AlertDialog.Builder(EX08_11.this).setTitle("Message")
.setMessage(mess)
.setNegativeButton("确定",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
})
.show();
}
}
php代码
$data = file_get_contents(''php://input'');
$time = date("YmdHis");
$rand = rand(0,100);
$filename = $_SERVER[''DOCUMENT_ROOT''].''/image/''.$time.$rand.''.jpg'';
while(file_exists($filename))
{
$filename = $_SERVER[''DOCUMENT_ROOT''].''/image/''.$time.rand(0,100).''.jpg'';
}
echo $filename;
$handle = fopen($filename, ''w'');
if ($handle)
{
fwrite($handle,$data);
fclose($handle);
echo "success";
}
?>
以上就介绍了android 上传图片到php服务器,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
android 客户端 与 php服务器通信
android客户端与php端如何进行身份验证, 在android上登陆后, 每次HttpPost都需要传值账号密码.再一次验证吗?.
今天的关于从PHP服务器获取图像和元数据到android和php获取服务器信息的分享已经结束,谢谢您的关注,如果想了解更多关于android – 如何从字节数组上传数据到PHP服务器上传进度?、android – 如何从服务器获取数据并使用它更新数据库?、android 上传图片到php服务器、android 客户端 与 php服务器通信的相关知识,请在本站进行查询。
本文标签: