Cannot get an image from API - image

i create an flutter application connected with (API), everything work good, and i can fetch all data from my API, but with image, i encounter a problem, in flutter i get the right URL for image from API, i try it and it is work good when i just paste the URL like this everything work good, and image displayed in mobile:
leading: Image.network("http://192.168.43.106:3000/cupload/0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png"),
this url (http://192.168.43.106:3000/cupload/0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png), i fetch it from API ..... by this code:
getCategories() async {
var res = await http.get("http://192.168.43.106:3000/cat");
if(res.statusCode == 200){
var jsonObject = json.decode(res.body);
print(jsonObject['result']);
return jsonObject['result'];
}
and then :
#override
void initState() {
getCategories();
super.initState();
}
#override
Widget build(BuildContext context) {
return Center(
child: FutureBuilder(
future: getCategories(),
builder: (context, snapshot){
if(snapshot.data != null){
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index){
return ListTile(
leading: Image.network(snapshot.data[index]['img']),/*
}
but as you see when i make
leading: Image.network(snapshot.data[index]['img'])
nothing display in mobile and i see this message:
I/flutter (25006): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter (25006): The following NetworkImageLoadException was thrown resolving an image codec:
I/flutter (25006): HTTP request failed, statusCode: 404,
I/flutter (25006): http://192.168.43.106:3000/cupload%5C0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (25006):
I/flutter (25006): When the exception was thrown, this was the stack:
I/flutter (25006): #0 NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:90:9)
I/flutter (25006): <asynchronous suspension>
I/flutter (25006): #1 NetworkImage.load (package:flutter/src/painting/_network_image_io.dart:47:14)
I/flutter (25006): #2 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
I/flutter (25006): #3 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
I/flutter (25006): #4 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:325:84)
when i add this code
print(snapshot.data[index]['img']);
inside (itemBuilder in the ListView) this is the result:
Restarted application in 1,428ms.
I/flutter (21905): http://192.168.43.106:3000/mo11111
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/upload\500_F_216231494_oJFKyTVOUoiFMzvhhetAcSPktyZwhp7L.jpg
I/flutter (21905): http://192.168.43.106:3000/cupload\Capture.PNG
I/flutter (21905): http://192.168.43.106:3000/cupload\1_ir8j2Ag89QZFAKBZDZWEUw.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\download (1).jfif
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/mo11111
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/upload\500_F_216231494_oJFKyTVOUoiFMzvhhetAcSPktyZwhp7L.jpg
I/flutter (21905): http://192.168.43.106:3000/cupload\Capture.PNG
I/flutter (21905): http://192.168.43.106:3000/cupload\1_ir8j2Ag89QZFAKBZDZWEUw.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\download (1).jfif
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following NetworkImageLoadException was thrown resolving an image codec:
HTTP request failed, statusCode: 404, http://192.168.43.106:3000/cupload%5C0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
When the exception was thrown, this was the stack:
#0 NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:90:9)
<asynchronous suspension>
#1 NetworkImage.load (package:flutter/src/painting/_network_image_io.dart:47:14)
#2 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
#3 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
...
Image provider: NetworkImage("http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png", scale: 1.0)
Image key: NetworkImage("http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (21905): http://192.168.43.106:3000/mo11111
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/upload\500_F_216231494_oJFKyTVOUoiFMzvhhetAcSPktyZwhp7L.jpg
I/flutter (21905): http://192.168.43.106:3000/cupload\Capture.PNG
I/flutter (21905): http://192.168.43.106:3000/cupload\1_ir8j2Ag89QZFAKBZDZWEUw.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\download (1).jfif
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
D/ViewRootImpl#10bbcbe[MainActivity](21905): ViewPostIme pointer 0
D/ViewRootImpl#10bbcbe[MainActivity](21905): ViewPostIme pointer 1
EDIT.......
i notice a problem, because in browser i got this url for example:
http://192.168.43.106:3000/cupload//1_ir8j2Ag89QZFAKBZDZWEUw.png
but in flutter it is like this:
http://192.168.43.106:3000/cupload%5C1_ir8j2Ag89QZFAKBZDZWEUw.png
there is the problem after (cupload)...
this is the code in node.js
const storage2 = multer.diskStorage({
destination: (req, file, cb)=>{
cb(null, 'cupload');
},
filename: (req, file, cb)=>{
cb(null, file.originalname);
}
});
const filter2 = (req, file, cb)=>{
if(file.mimetype == "image/jpg" || file.mimetype == "image/jpeg" || file.mimetype == "image/png"){
cb(null, true)
}else{
cb(null, false);
}
}
const cupload = multer({
storage: storage2,
filter: filter2
});
app.post('/cat', cupload.single('img'));
app.use('/cupload', express.static('cupload'));

please try using r'snapshot.data[index]['img']' as an Image URL

Related

How to know end of a single event of Server-Sent Event?

I have for example, the following server code:
emitter.send(SseEmitter.event().id("id").name("eventName").data(dummyData.getName()));
delay(10);
emitter.send(SseEmitter.event().id("id").name("eventBalance").data(dummyData.getBalance()));
delay(5000);
emitter.send(SseEmitter.event().id("id").name("eventHistory").data(dummyData.getHistory()));
emitter.complete();
When I receive it on client side (I use flutter, but I believe that does not matter), I get the events like this:
I/flutter (17221): id:id
I/flutter (17221): event:eventName
I/flutter (17221): data:Joe
I/flutter (17221):
I/flutter (17221): id:id
I/flutter (17221): event:eventBalance
I/flutter (17221): data:2511
I/flutter (17221):
I/flutter (17221): id:id
I/flutter (17221): event:eventHistory
I/flutter (17221): data:[{"dateTime":"2022-04-22T19:01:38.184845+01:00","value":"somevalue"}]
I/flutter (17221):
I/flutter (17221): done
I printed only the body here. As far as I can see, the only thing that signals an event is sent completely in the stream (all of this is in a single stream) is an emppy line. But what if the request had an empty line too?
I found that Server-Sent Events use double newline to separate events, there is no other way to know.
Source: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format

Laravel backend doesn't send data to Vuejs frontend. how I resolve this?

I used vuejs frontend and Laravel backend for my application. It perfectly worked on Localhost. but when I deployed in shared hosting, it says this error on the browser console log
vue-resource.esm.js:996 GET http://regback.dazkon.com/api/getStudent 500 (Internal Server Error)
/student:1 Uncaught (in promise)
e {url: 'http://regback.dazkon.com/api/getStudent', ok: false, status: 500, statusText: 'Internal Server Error', headers: e, …}
body: {message: 'Class "PDO" not found', exception: 'Error', file: '/home2/dazkonco/regback.dazkon.com/vendor/laravel/framework/src/Illuminate/Database/Connection.php', line: 1431, trace: Array(51)}
bodyText: "{\n \"message\": \"Class \\\"PDO\\\" not found
headers: e {map: {…}}
ok: false
status: 500
statusText: "Internal Server Error"
url: "http://regback.dazkon.com/api/getStudent"
data: (...)
[[Prototype]]: Object

Intermittent Server cannot set status after HTTP headers have been sent

I am getting an intermittent error in the event logs "Server cannot set status after HTTP headers have been sent". The webapi site is working fine and delivering the correct output even when this happens.
Exception information:
Exception type: HttpException
Exception message: Server cannot set status after HTTP headers have been sent. at System.Web.HttpResponse.set_StatusCode(Int32
value) at System.Web.HttpResponseWrapper.set_StatusCode(Int32
value) at
System.Web.Http.WebHost.HttpControllerHandler.d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
System.Web.Http.WebHost.HttpControllerHandler.d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
System.Web.Http.WebHost.HttpControllerHandler.d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at System.Web.TaskAsyncHelper.EndTask(IAsyncResult ar) at
System.Web.HttpTaskAsyncHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult
result) at
System.Web.HttpApplication.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult
ar) at
System.Web.HttpApplication.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult
ar)
The response object is manipulated directly (the format of the json is not known until runtime) as follows.
var response= HttpContext.Current.Response;
response.StatusCode = (int) HttpStatusCode.OK;
response.Headers.Add("X-Robots-Tag", "noindex, nofollow");
response.Write("Some json");
response.Flush();
response.End();
response.End is required, I do not understand why, a similar error occurs if this line is missed out.
Why does this happen, and is there a better way of doing it?
Since you tagged your question with asp.net-web-api I assume that you are using that code in an api controller method. If so, I suggest returning the unknown Json like this:
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("Some json", System.Text.Encoding.UTF8, "application/json")
};
response.Headers.Add("X-Robots-Tag", "noindex, nofollow");
return response;
Above code was taken from the following links and adapted:
Put content in HttpResponseMessage object
and
Add a custom response header in ApiController (questions and answers on SO)

jquery ajax call returning error when response text is string

I am making an ajax requests with following details:
Request URL:http://localhost:8080/MyWebService/cars/70ced046-3061-4d9d-b9ce-7d1291d5b5c0
Request Method:DELETE
Status Code:200 OK
Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Content-Type:application/json
Host:localhost:8080
Origin:http://localhost:8080
Pragma:no-cache
Proxy-Connection:keep-alive
Referer:http://localhost:8080/MyWebService/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11
X-Requested-With:XMLHttpRequest
Response Headers
Content-Length:36
Content-Type:application/json
Date:Tue, 10 Jul 2012 10:18:45 GMT
Server:Apache-Coyote/1.1
But then the ajax call returns error with the following xhr,status and error objects:
function(request, status, error)
>status
"parsererror"
>error
SyntaxError
arguments: Array[0]
get message: function () { [native code] }
get stack: function () { [native code] }
set message: function () { [native code] }
set stack: function () { [native code] }
type: "unexpected_token_number"
__proto__: Error
>request
Object
abort: function ( statusText ) {
always: function () {
complete: function () {
done: function () {
error: function () {
fail: function () {
getAllResponseHeaders: function () {
getResponseHeader: function ( key ) {
isRejected: function () {
isResolved: function () {
overrideMimeType: function ( type ) {
pipe: function ( fnDone, fnFail, fnProgress ) {
progress: function () {
promise: function ( obj ) {
readyState: 4
responseText: "44550569-871d-49ae-8583-f07ee3a07832"
setRequestHeader: function ( name, value ) {
state: function () {
status: 200
statusCode: function ( map ) {
statusText: "OK"
success: function () {
then: function ( doneCallbacks, failCallbacks, progressCallbacks ) {
__proto__: Object
If the response is an integer or any other json, the call works just fine entering the success function. But if the response is string (44550569-871d-49ae-8583-f07ee3a07832) it gives error.
Any suggestions on how to fix this?
Because you are expecting the response to be a JSON
Content-Type:application/json
Paste your ajax calling javascript part, so that I can provide you more information.
The problem was on the server side where i was forcing the server to return application/json:
#RequestMapping(method = RequestMethod.DELETE, value = "/cars/{id}", produces="application/json")
which produces the following response header:
Response Headers
Content-Length:36
Content-Type:application/json
Date:Tue, 10 Jul 2012 10:18:45 GMT
Server:Apache-Coyote/1.1
Now that i remove the "produces="application/json"" part, it returns following header:
Content-Length:36
Content-Type:text/plain;charset=ISO-8859-1
Date:Wed, 11 Jul 2012 06:08:00 GMT
Server:Apache-Coyote/1.1

WebRequest time out error

I tried to build an application in Mono using the setting "Mono 2.10.2", but the the following errors.
Unhandled Exception: System.Net.WebException: The request timed out
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00046] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\System\System.Net\HttpWebRequest.cs:813
at System.Net.HttpWebRequest.GetResponse () [0x0000e] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\System\System.Net\HttpWebRequest.cs:825
The bit of code causing this issue is:
string url = "http://webFarm/config.prop";
WebRequest req = WebRequest.Create(url);
WebResponse resp = req.GetResponse();
It failed immediately. But if I switch the setting to "Default Runtime" instead of "Mono 2.10.2", everything is OK. Could anyone help me with this?
Regards,
David
I'm also getting this error using the code:
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("https://api-3t.sandbox.paypal.com/nvp");
objRequest.Timeout =15000;
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
using (StreamWriter myWriter = new
StreamWriter(objRequest.GetRequestStream()))
myWriter.Write(strPost);
causes exception at GetRequestStream() :
System.Net.WebException: The request timed out
at System.Net.HttpWebRequest.GetRequestStream () [0x0005f] in
/usr/src/redhat/BUILD/mono-2.10.2/mcs/class/System/System.Net/HttpWebRequest.cs:752
at NVPAPICaller.HttpCall (System.String NvpRequest) [0x00000] in <filename
unknown>:0
--------------------------------------------------------------------------------
Version information: Mono Runtime Version: 2.10.2 (tarball Mon Apr 18
18:57:39 UTC 2011); ASP.NET Version: 2.0.50727.1433
You should revert back to older Mono 2.10 , 2.10.1 will probably work.

Resources