Answer the question
In order to leave comments, you need to log in
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
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")
How to convert request request from PHP to Java (under Android)?
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) {
// Ошибка :(
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question