com.android.codg.rentadeactivos.TestWebServer.java Source code

Java tutorial

Introduction

Here is the source code for com.android.codg.rentadeactivos.TestWebServer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.android.codg.rentadeactivos;

import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.*;
//import com.squareup.okhttp.OkHttpClient;
//import com.squareup.okhttp.Request;
//import com.squareup.okhttp.RequestBody;
//import com.squareup.okhttp.Response;
import java.net.MalformedURLException;
import java.net.URL;

/**
 *
 * @author Mac
 */
public class TestWebServer {

    OkHttpClient webClient = new OkHttpClient();

    public String getString1(String metodo, RequestBody formBody) {

        try {
            URL url = new URL("http://192.168.0.16:5000/" + metodo);
            Request request = new Request.Builder().url(url).post(formBody).build();
            Response response = webClient.newCall(request).execute();//Aqui obtiene la respuesta en dado caso si hayas pues un return en python
            String response_string = response.body().string();//y este seria el string de las respuesta
            //System.out.println(response_string);
            return response_string;
        } catch (MalformedURLException ex) {
            return "aldo";
        } catch (Exception ex) {
            String mensaje = ex.toString();
            return mensaje;
        }
    }

}