I have PHP Server where i will send send request from client side and my request is going successfully but i want response from server which should be an URL and when i am trying to retrieve response(URL as response) from server then it is coming successfully but i am getting an Exception onto my StringContent Object. That is: =============================================================================
{System.UriFormatException: Invalid URI: The format of the URI could not be determined.
at System.Uri.CreateThis (System.String uri, System.Boolean dontEscape, System.UriKind uriKind) [0x0007b] in <ca7419b40e504a6dbe088f6fe95d09aa>:0
at System.Uri..ctor (System.String uriString, System.UriKind uriKind) [0x00014] in <ca7419b40e504a6dbe088f6fe95d09aa>:0
at MPTrain.view.ProductList.SendRequest () [0x001aa] in C:\nginx\www\repos\xformsexperimental\MPTrain\MPTrain\MPTrain\view\ProductList.xaml.cs:111 }
=============================================================================
I have tried in my server code:
$sql = "CALL FetchImage()";
$res = mysqli_query($conn,$sql);
if($res == TRUE)
{
if (mysqli_num_rows($res) > 0)
{
// output data of each row
while($row = mysqli_fetch_assoc($res)) {
echo json_encode($row);
}
}
}
This is my client side code:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://192.168.1.011/repos/xformsexperimental/RestApiTrain/index.php");
Dictionary<object, object> keyValuePairs = new Dictionary<object, object>();
keyValuePairs.Add("eml", "Manisha");
var jsonData = JsonConvert.SerializeObject(keyValuePairs);
var content = new StringContent(jsonData, UnicodeEncoding.UTF8, "application/json");
var posttask = await client.PostAsync(client.BaseAddress.ToString(), content); //accessing response
var stringContent = await posttask.Content.ReadAsStringAsync();
// ResponseImage responseImage = JsonConvert.DeserializeObject<ResponseImage>(stringContent);
Uri uri = new Uri(stringContent,UriKind.Absolute);
My response which i am getting from server is:
=============================================================================
{\"image\":\"http:\\/\\/localhost\\/example\\/images\\/pic1.png\"}
=============================================================================
And i want an exact or absolute URL from server. How i can get it please help...
I tried This:
ResponseImage responseImage = JsonConvert.DeserializeObject<ResponseImage>(stringContent);
but i am getting this Exception onto stringContent
================================================================================ {Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: {. Path '', line 3, position 0. at Newtonsoft.Json.JsonTextReader.Read () [0x000c7] in <12891e825fce44a581e5bbbb579c1d49>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in <12891e825fce44a581e5bbbb579c1d49>:0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00054] in <12891e825fce44a581e5bbbb579c1d49>:0 at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <12891e825fce44a581e5bbbb579c1d49>:0 at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in <12891e825fce44a581e5bbbb579c1d49>:0 at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <12891e825fc e44a581e5bbbb579c1d49>:0 at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in <12891e825fce44a581e5bbbb579c1d49>:0
The response appears to be JSON.
It appears you tried to have a strongly typed model so based on the shown JSON, the model should look like this
public class ResponseImage {
public string image { get; set; }
}
in order to match the expected data
Parse the response and extract the desired information.
//...
ResponseImage responseImage = JsonConvert.DeserializeObject<ResponseImage>(stringContent);
Uri uri = new Uri(responseImage.image, UriKind.Absolute);
Now based on the server code
// output data of each row
while($row = mysqli_fetch_assoc($res)) {
echo json_encode($row);
}
This has the potential to return malformed JSON if there are more than one row in the collection.
I would suggest you populate a proper collection and then return that as a JSON array.
//...
if (mysqli_num_rows($res) > 0) {
$result = array();
// collect data of each row
while($row = mysqli_fetch_assoc($res)) {
$result[] = $row; //push data into array
}
echo json_encode($result);
}
//...
The client side code would then need to be updated to expected a collection instead of a single object.
ResponseImage[] responseImages = JsonConvert.DeserializeObject<ResponseImage[]>(stringContent);
Uri[] uris = responseImages.Select(x => new Uri(x.image, UriKind.Absolute)).ToArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.