Android - How to upload a txt file to a website?

I want to upload a txt file to a website, I agree that I have not studied it in any detail, but I have looked at a few examples and would like more experienced opinions about the right direction.

Here is what I still have:


DefaultHttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
private String ret;

HttpResponse response = null;
HttpPost httpPost = null;


public String postPage(String url, String data, boolean returnAddr) {

    ret = null;

    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

    httpPost = new HttpPost(url);
    response = null;

    StringEntity tmp = null;         

    try {
        tmp = new StringEntity(data,"UTF-8");
    } catch (UnsupportedEncodingException e) {
        System.out.println("HTTPHelp : UnsupportedEncodingException : "+e);
    }

    httpPost.setEntity(tmp);

    try {
        response = httpClient.execute(httpPost,localContext);
    } catch (ClientProtocolException e) {
        System.out.println("HTTPHelp : ClientProtocolException : "+e);
    } catch (IOException e) {
        System.out.println("HTTPHelp : IOException : "+e);
    } 
            ret = response.getStatusLine().toString();

            return ret;
}

And I call it this way:


postPage("http://www.testwebsite.com", "data/data/com.testxmlpost.xml/files/logging.txt", true));

I want to upload a file from the device to the website.

But when I try this path, I get the following answer back.


HTTP/1.1 405 Method Not Allowed

Am I trying to make the right path or should I do it differently?

+3
source share
1 answer

This code looks reasonable, the error is from the server and indicates that POST is not allowed for this page.

"data/data/com.testxmlpost.xml/files/logging.txt". , FileEntity.

+3

Source: https://habr.com/ru/post/1749632/


All Articles