Skip to content

Instantly share code, notes, and snippets.

@prasad091
Last active January 31, 2017 09:18
Show Gist options
  • Save prasad091/810831ff9eae32a4d52bf8774940a5c7 to your computer and use it in GitHub Desktop.
Save prasad091/810831ff9eae32a4d52bf8774940a5c7 to your computer and use it in GitHub Desktop.
<?php
$DB_SERVER = "localhost";
$DB_USER = "root";
$DB_PASS = "";
$DB_NAME ="kgchatdb";
$conn = mysql_connect($DB_SERVER,$DB_USER,$DB_PASS) or die('localhost connection problem'.mysql_error());
mysql_select_db($DB_NAME, $conn);
?>
<?php
include_once 'config.php';
if($_SERVER['REQUEST_METHOD']=='POST'){
$target_dir = "profile_images/";
$target_file_name = $target_dir .basename($_FILES["file"]["name"]);
$deviceId = $_POST['deviceId'];
$sql = "update contact_registration set contact_image = '$target_file_name' where deviceid = '$deviceId'";
echo $sql;
$response = array();
// Check if image file is a actual image or fake image
if (isset($_FILES["file"]))
{
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file_name))
{
$success = true;
$message = "Successfully Uploaded";
}
else
{
$success = false;
$message = "Error while uploading";
}
}
else
{
$success = false;
$message = "Required Field Missing";
}
$response["success"] = $success;
$response["message"] = $message;
echo json_encode($response);
if (!pg_query($sql)) { // Error handling
echo "Something went wrong! :(";
}
echo 'Connected successfully';
pg_close($link);
}
?>
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
public interface ApiService {
@NonNull
private RequestBody createPartFromString(String descriptionString) {
return RequestBody.create(
MediaType.parse(MULTIPART_FORM_DATA), descriptionString);
}
@Multipart
@POST("chats/v1/rest/imageChat.php")
Call<ResponseBody> uploadImageWithPartMap(
@PartMap() Map<String, RequestBody> partMap,
@Part MultipartBody.Part file);
}
public void uploadFile(String fileUrl,String type) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
// Change base URL to your upload server URL.
Retrofit build = new Retrofit.Builder()
.baseUrl(Config.EMULATOR_ENDPOINT)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ApiService service = build.create(ApiService.class);
File file = new File(fileUrl);
String regId = getRegId();
RequestBody reqFile = null;
if(type.equals("image")) {
reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
}else if(type.equals("voice")) {
reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), fileUrl);
}
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), reqFile);
// create a map of data to pass along
RequestBody regIdReq = createPartFromString(regId);
RequestBody fileUrlReq = createPartFromString(fileUrl);
RequestBody selectedContact = createPartFromString(getContactSelected());
RequestBody titleReq = null;
RequestBody messageReq = null;
if(type.equals("image")) {
titleReq = createPartFromString("Image");
messageReq = createPartFromString("new images");
}else if(type.equals("voice")) {
titleReq = createPartFromString("Voice");
messageReq = createPartFromString("voice message");
}
RequestBody typeReq = createPartFromString(type);
HashMap<String, RequestBody> map = new HashMap<>();
map.put("regId", regIdReq);
map.put("message", messageReq);
map.put("fileUrl", fileUrlReq);
map.put("title",titleReq);
map.put("type",typeReq);
map.put("contactSelectedId",selectedContact);
Call<ResponseBody> call = null;
if(type.equals("image")) {
call = service.uploadImageWithPartMap(map, body);
}else if(type.equals("voice")) {
call = service.uploadVoiceWithPartMap(map, body);
}
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
// Do
if(response.isSuccessful()){
System.out.println(response.body().toString()+":Successful");
System.out.println("Url:"+response.raw().request().url());
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
onErrorDialog("No Internet",mainActivity.getString(R.string.network_error));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment