How to send image/pdffile from android to android with database - android

I want to try developing an android application that can send image/pdfFile from android to android device. For example i have a list of patients. when i click an item it will go to send file to patient that i have clicked. Then the sent file will be stored in database. and will serve as sent file history. Just like an instant messaging. I found an example of instant messaging but this only sending texts not images/files
here AndroidIM
Could anyone convert it to sending images?I really don't know how. Or just link me to some tutorial in sending images/files to another device. I am using also database for storing history of sent files.

send the image to you PHP backend using the Http MultiPart Request like the following :
try{
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(imagePath);
ContentBody encFile = new FileBody(file,"image/png");
entity.addPart("images", encFile);
request.setEntity(entity);
}catch(Exception e ){
}
and please give me some feedback .
Hope That Helps .

Related

How to get any data from site for android app

I'm trying to create android application in which user will be receiving notification every hour with some short string info on his choice, like some random word from urban dictionary, or some random grammar rule. But the problem is I cant find any tips about how to get that random info and parse it into my app. For example urban dictionary have api only for getting results by Word query. Maybe there is any sites with useful info that have api which will be easier to use in my app or maybe i should use any other solutions?
You can scrape the website. Use below code to get the website in html format and scrape it using JSoup
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://www.urbandictionary.com");
ResponseHandler<String> handler = new BasicResponseHandler();
String response = httpClient.execute(httpGet, resHandler);
// response has the website in html format
But make sure you contact the website and let them know you are doing it. I am not sure about the legality behind it.

Get data from phpmyadmin using another machine

I am building an android app with the following code:
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/tut.php");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
System.out.println("Exception 1 caugt");
}
This apps works fine in my computer.
I want everyone download this app can use it and read the data from phpmyadmin.
can any one teach my how to do this?
(I want the data in phpmyadmin can be read from public user.)
And do I need to change the code?
HttpPost httpPost = new HttpPost("http://XXXXXXXXX/tut.php");
To make your app available to everyone you have to host your data online. There are many ways you can achieve this and it depends a lot on what type of application you intend to distribute.
If you just want to start trying out how things work you can buy a normal web hosting which supports mysql and php. This will cost you between 30 to 60 euros for a year. You setup your database, upload your php api and your good to go.
If you want a more professional approach you can choose to host in the cloud via cloud services the like of Amazon Web Services and Microsoft's Azure. This has a big learning curve but it gives you total control over the server and also huge experience.

How to send data from android to SQL Server 2008?

I have created one web site and android applcation and I have used backed as SQL Server 2008.
I have used web services to display data from server to android application for that i have used JSON and ksoap library and it is working fine. I can display data from server to android. But now i want to store data from android application to SQL Server 2008. So anyone please tell me the step or how i can do that. Please help.
You can connect to SQL Server database through webservice.
Here is the sample code to call the webservice.
String url = "your_webservice_URL";
try
{
HttpPost loginHttpPost = new HttpPost(url);
HttpContext localContext = new BasicHttpContext();
MultipartEntity multipartContent = new MultipartEntity();
multipartContent.addPart("parameter1", new StringBody(value1));
multipartContent.addPart("parameter2", new StringBody(value2));
loginHttpPost.setEntity(multipartContent);
HttpClient objHttpClient = new DefaultHttpClient();
HttpResponse response = objHttpClient.execute(loginHttpPost,localContext);
}
catch (IOException e) {
e.printStackTrace();}
Here is the example source code to Connect Android to SQL sever via WCF using JSON
Example :
1. Connecting Android to SQL Server
2. Get Data using XML
3. Get Data using JSON
- Inserting Data into database using WCF Service

HTTP File Download 500 internal server error

I'm writing an Android app which does exactly the same as our iPad app for our company. But I have 1 issue while developing on the android. The app downloads a file from a webserver. It will call an URL like:
https://www.somedomain.com/API/Download.aspx?param1=test&param2=test2 etc...
On the iPad this is working perfectly (I use the ASIHTTPRequest class for this). But on Android it is giving me only problems.
As soon as I want to download the file with the android, it downloads a file with a 500 internal server error HTML document instead of the PDF file.
I've checked the URL's, they look exactly the same as on the iPad.
The only thing I can imagine, is that the file which the user downloads is created "on the fly". So it takes some time (10 or 20 sec) to generate the file, and then the file is being downloaded.
On android I do this:
I have a class which extends:
extends AsyncTask<String, Integer, JSONObject>
In a method, I do this:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
InputStream data = response.getEntity().getContent();
File file = new File(context.getDir("docs", Context.MODE_PRIVATE), FileName);
OutputStream output = new FileOutputStream(file);
ByteStreams.copy(data, output);
Closeables.closeQuietly(output);
But this is giving me a 500 internal server error doc instead of the desired PDF file. What am I missing here? (Sorry, I just started developing for Android so I'm not an expert in this case ;-))
Thanks in advance!
The fact that your response status code from the server is the problem means that this should have nothing to do with the Android stuff and only to do with the request you're sending. I notice, though this might be due to intentional omission, that you're doing a POST request without adding any POST params. Should this be a GET? I notice that the endpoint is an ASPX path with GET params in the query string. Maybe your server is set up to only respond to GET and not POST. How is this being done in the iOS code? Is there no differentiation between GET and POST, or is this abstracted from you via the library you're using?
Ok it works now... Stupid thing... I constantly created a new HttpClient so the session was not stored among connections. That is why the server returned a 500 internal server error because the user was not known by the server...
Thanks everyone for your help though!

How to attach a file to the post url in Android

I am developing a Helpdesk application, in this app I am able to read and reply for the tickets sent by the customer. Now I got a requirement, I have to upload a file also. I have a post url to reply for the ticket, I will use Namevaluepairs to attach message with the url..
nameValuepair("id",ticketId);(nameValuepair is the instance of BasicNameValuePair)
nameValuepair("subject",subject);
nameValuepair("reply",message);
If file is not there then it will be like the above code. If file is there I one more parameter comes for file. What I have to do is to attach a file and encode it and then include it into Namevaluepair...
nameValuepair("file",encodedfile);
How can I Upload the file to this app and encode it.
I got some resources to attach a file to the default android email client using email intent, that was not helpful for me.
Thanks in Advance
Uploading a file is more complicated than adding a name-value pair. You need to get the output stream of the request and write the file. You can find an example here.
Use the Http client by Apache and its POST method:
DefaultHttpClient httpclient = appContext.GetHttpClient();
HttpPost httppost = new HttpPost("your.url.com");
httppost.setEntity(new FileEntity(yourFile, PLAIN_TEXT_TYPE));
HttpResponse response = httpclient.execute(httppost);
InputStream responseStream = new ByteArrayInputStream(responseString.getBytes("UTF-8"));

Categories

Resources