나는 기본적으로 안드로이드에서 이미지를 업로드하고 PHP 서버에 업로드하려고하지만 여기는 내가이 코드 또는 이미지 업로드
와 어떤 연결도 안하고있어. 이 오류가 발생합니다.PHP 서버에 이미지를 업로드하고 phpmyadmin에 저장하는 방법
Error in http connection java.net.UnknownHostException: host name
하지만 내 지식에 따라 올바른 연결과 php 파일을 올바른 도메인에 제공합니다. 내 코드에서 봐 : UploadImage.java
public class UploadImage extends Activity {
InputStream inputStream;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://server.com/uploadimage/uploadimage.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
Toast.makeText(UploadImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
System.out.println("Error in http connection "+e.toString());
}
}
public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{
String res = "";
StringBuffer buffer = new StringBuffer();
inputStream = response.getEntity().getContent();
int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
Toast.makeText(UploadImage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();
if (contentLength < 0){
}
else{
byte[] data = new byte[512];
int len = 0;
try
{
while (-1 != (len = inputStream.read(data)))
{
buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer…..
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
inputStream.close(); // closing the stream…..
}
catch (IOException e)
{
e.printStackTrace();
}
res = buffer.toString(); // converting stringbuffer to string…..
Toast.makeText(UploadImage.this, "Result : " + res, Toast.LENGTH_LONG).show();
//System.out.println("Response => " + EntityUtils.toString(response.getEntity()));
}
return res;
}
}
의 PHP 코드 :이 문제를 알고
<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';?>
어느 하나? 하나는 PHP 파일에서 MySQL의 데이터베이스에 저장하고 viceversa에 가져 오는 방법을 알고있는 경우
Error in http connection java.net.UnknownHostException: host name
는
HttpPost
연결을 할 수 없다는 것을 의미합니다 ... 문제는 매우 분명하다 ... 여기
imagecreatefromstring() 함수를 참고 uploadimage.php 잘 작동합니다. 에뮬레이터의 inbuilt 브라우저를 열어 인터넷 연결을 확인할 수도 있습니다. – Dharmendra