D
D
Dmitry Richter2014-07-15 15:25:19
PHP
Dmitry Richter, 2014-07-15 15:25:19

How to convert request request from PHP to Java (under Android)?

In general, I have never worked with php, but there is a request, it seems to be simple, but I don’t know how to implement it in android studio, maybe through a post request?

$ip = '80.82.45.226';      //Ваш ip адрес
            $port = 8089;           //Ваш порт
            $secret = 'llksdjhKJHKJjhjdaf';     //Ваш секретный ключ
        //  $crew_group_id = 59;    //Группа экипажей

            function autoloader($classname) {
                $dir = dirname(__FILE__);
                if(strpos($classname, 'Request') !== false) {
                    include $dir . '/Request/' . $classname . '.php'; //Указать правильную папку!
                } else if (strpos($classname, 'Response') !== false) {
                    include $dir . '/Response/' . $classname . '.php'; //Указать правильную папку!
                }
            }

            spl_autoload_register('autoloader');


            $request = new CreateOrderRequest($ip, $port, $secret);

            $request
                ->setPhone($_POST["Number"])
                ->setCustomer($_POST["OrderClientName"])
                ->setSource($_POST["OrderSourceAddress"])
                ->setDest($_POST["OrderDestAddress"])
                ->setComment($_POST["OrderComment"])
                ->setCrewGroupId($_POST["СrewGroupId"])
                ;

            $request->send();

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FanKiLL, 2014-07-15
@followthemoney

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpSend {
  public static void main(String[] args) {
    System.out.println(SendJsonViaPost("http://toster.ru/", "ваш json"));
  }

  public static String SendJsonViaPost(String url, String json) {
    String responseJson = "";
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    try {
      StringEntity stringEntity = new StringEntity(json, "UTF-8");
      stringEntity.setContentType("application/json");
      post.setEntity(stringEntity);
      HttpResponse response = client.execute(post);
      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
      String line = "";
      while ((line = rd.readLine()) != null) {
        responseJson += line;
      }
      
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    return responseJson;
  }
}

HttpSend.sendJsonViaPost("http://toster.ru/", "ваш json")

S
Sergey, 2014-07-15
Protko @Fesor

How to convert request request from PHP to Java (under Android)?

in fact, nothing, only if your device has a dedicated IP.
The code given by you as I understand for MundiPagg? Then as I understand you need to make a request to their server.
In general, try to figure out what you need to do, but how - these are already trifles.

A
Alexander, 2014-07-15
@SashaSkot

public void postData() {
    // Создадим HttpClient и PostHandler
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.moisaitik.ru/postdemo.php");

    try {
        // Добавим данные (пара - "название - значение")
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("login", "andro"));
        nameValuePairs.add(new BasicNameValuePair("text", "Привет!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Выполним запрос
        HttpResponse response = httpclient.execute(httppost);
        
    } catch (ClientProtocolException e) {
        // Ошибка :(
    } catch (IOException e) {
        // Ошибка :(
    }
}

1 of the examples from a google query. It's not clear from your code where the secret goes. You need to look at the class constructor. Most likely in the header.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question